Android 填充listview时发生JSON错误
我有以下代码:Android 填充listview时发生JSON错误,android,json,listview,Android,Json,Listview,我有以下代码: package com.example.test.center; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity;
package com.example.test.center;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Posts extends Activity {
String type,firstname,lastname,email,number,username;
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs;
HttpResponse response;
HttpEntity entity;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle gotBasket=getIntent().getExtras();
type=gotBasket.getString("type_post");
this.setTitle(type);
setContentView(R.layout.addpost);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
httpclient = new DefaultHttpClient();
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://192.168.1.38/LTC/Uploadposts.php?type="+type+"");
try{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("type",type));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200)
{
entity=response.getEntity();
if(entity !=null)
{
InputStream instream=entity.getContent();
JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
//Toast.makeText(getBaseContext(),jsonResponse.toString(),Toast.LENGTH_LONG).show();
String retUser=jsonResponse.getString("username");// username is the name of a column
String retcont=jsonResponse.getString("Content");
String getf=jsonResponse.getString("FirstN");
String getl=jsonResponse.getString("LastN");
String dop=jsonResponse.getString("Dateofp");
try {
JSONArray jArray = new JSONArray(jsonResponse.toString());
int jArrayLength = jArray.length();
List<String> listContents = new ArrayList<String>(jArrayLength);
for(int i =0; i<jArray.length(); i++){
JSONObject json_data = jArray.getJSONObject(i);
listContents.add(json_data.getString("username"));
}
ListView myListView = (ListView) findViewById(R.id.listView2);
myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContents));
}catch(JSONException e){
Log.e("log_tag","Error parsin data "+e.toString());
}
}}
}//End of first try
catch(Exception e)
{
Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();
} // End of first catch
} //End of oncreate bundle
private static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}catch(IOException e)
{
e.printStackTrace();
}
finally
{
try{
is.close();
}catch(IOException e)
{
e.printStackTrace();
}
}return sb.toString();
}
} //End of Class
package com.example.test.center;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.util.EntityUtils;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.util.Log;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
公共类职位扩大了活动范围{
字符串类型、名字、姓氏、电子邮件、号码、用户名;
HttpClient-HttpClient;
HttpPost-HttpPost;
ArrayList名称值对;
HttpResponse响应;
http实体;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
Bundle-gotbarket=getIntent().getExtras();
type=getbasket.getString(“type_post”);
此.setTitle(类型);
setContentView(R.layout.addpost);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
httpclient=新的DefaultHttpClient();
httpclient=新的DefaultHttpClient();
httppost=新的httppost(“http://192.168.1.38/LTC/Uploadposts.php?type=“+类型+”);
试一试{
nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“type”,type));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
if(response.getStatusLine().getStatusCode()==200)
{
entity=response.getEntity();
如果(实体!=null)
{
InputStream instream=entity.getContent();
JSONObject jsonResponse=新的JSONObject(convertStreamToString(instream));
//Toast.makeText(getBaseContext(),jsonResponse.toString(),Toast.LENGTH_LONG.show();
String retUser=jsonResponse.getString(“username”);//username是列的名称
String retcont=jsonResponse.getString(“内容”);
字符串getf=jsonResponse.getString(“FirstN”);
字符串getl=jsonResponse.getString(“LastN”);
String dop=jsonResponse.getString(“Dateofp”);
试一试{
JSONArray jArray=newjsonarray(jsonResponse.toString());
int jArrayLength=jArray.length();
List listContents=新的ArrayList(jArrayLength);
对于(int i=0;i对于要工作的上述代码,请将json响应用[]括起来……您将获得一个JSonObject,并尝试将其解析为JSONARRAY。。。
因此,要使上述代码工作,请将响应转换为
[{"username":"you","Dateofp":"2013-03-03","Content":"test1","IDpost:"1","LastN":" Yaghi","Type":"CARS","FirstN":"Mounzer"}]
或者使用简单的JSON对象解析并在listContents中添加对象。删除与
杰索纳雷
改变
{"IDpost":"1","username":"you","FirstN":"Mounzer","LastN":"Yaghi","Content":"test1","Type":"CARS","Dateofp":"2013-03-03"}{"IDpost":"2","username":"boss","FirstN":"Marwan","LastN":"Geha","Content":"test2\r\n","Type":"CARS","Dateofp":"2013-03-05"}
到
注意,在两个对象之间添加了“[”“]”和“,”。您不能这样做:JSONArray jArray=newjsonarray(jsonResponse.toString());
jsonResponse包含JSONObject而不是JSONArray发布日志时出现了什么错误?我无法理解为什么添加了php
标记,因为您没有显示任何与之相关的脚本。同时发布JSON字符串这是错误:error parsin data org.JSON.JSONException:Value{“username”:“you”,“Dateofp”:“2013-03-03”,“Content”:“test1”,IDpost:“1”,“LastN:“Yaghi”,“Type:“CARS”,“FirstN:“Mounzer”}无法将org.json.JSONobject类型的转换为jsonarray@moonwalker:发布您从服务器获取的完整json字符串,或与您共享从中获取的链接。您正在检索JSONTANKS。它确实很有效,但我从表中仅获取一行。该表包含两行,表示服务器端问题,从您的服务器发送一行或多行从服务器u发送两行,在android中显示一行?从服务器我发送两行,但在android中只取一行。我只在代码中做了更改:JSONArray jArray=new JSONArray(“[”+jsonResponse.toString()+“]);什么abt添加COMA“,”在两个对象之间??json数组结构需要一个“,”在btw对象中..它应该在{“IDpost”:“2”之前,在“Dateofp”:“2013-03-03”}之后。我告诉过你,我在上面的代码中所做的唯一更改是:JSONArray jArray=new JSONArray(“[”+jsonResponse.toString()+“]);我应该在哪里添加COMA?
[{"IDpost":"1","username":"you","FirstN":"Mounzer","LastN":"Yaghi","Content":"test1","Type":"CARS","Dateofp":"2013-03-03"},{"IDpost":"2","username":"boss","FirstN":"Marwan","LastN":"Geha","Content":"test2\r\n","Type":"CARS","Dateofp":"2013-03-05"}]