我有一个 php 文件,它以 json 格式将以下内容返回给 android:
[
{
"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",
"Type":"CARS",
"Dateofp":"2013-03-05"
},
{
"IDpost":"4",
"username":"boss",
"FirstN":"Marwan",
"LastN":"Geha",
"Content":"hello this is getting boring",
"Type":"CARS",
"Dateofp":"2000-02-02"
}
]
我想要做的是在其中一些信息中填充一个列表视图,但由于一些 json 异常,它不起作用,这是我的 java 代码:
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.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class Posts extends Activity {
String type, firstname, lastname, email, number, username, content;
HttpClient httpclient;
HttpPost httppost;
ArrayList<NameValuePair> nameValuePairs;
HttpResponse response;
HttpEntity entity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addpost);
Bundle gotBasket = getIntent().getExtras();
type = gotBasket.getString("type_post");
firstname = gotBasket.getString("fn");
lastname = gotBasket.getString("ln");
email = gotBasket.getString("pemail");
number = gotBasket.getString("number");
username = gotBasket.getString("user");
this.setTitle(type);
Button addp = (Button) findViewById(R.id.addpost1);
addp.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Bundle basket = new Bundle();
basket.putString("user", username);
basket.putString("fn", firstname);
basket.putString("ln", lastname);
basket.putString("number", number);
basket.putString("pemail", email);
basket.putString("type_post", type);
Intent a = new Intent(Posts.this, Addpost_2.class);
a.putExtras(basket);
startActivity(a);
}
});
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));
nameValuePairs.add(new BasicNameValuePair("FirstN",firstname));
nameValuePairs.add(new BasicNameValuePair("LastN",lastname));
nameValuePairs.add(new BasicNameValuePair("Content",content));
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");
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(convertStreamToString(instream));
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("FirstN") + " "
+ json_data.getString("LastN") + " "
+ json_data.getString("Dateofp")
+ "\n\n" +json_data.getString("Content"));
}
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());
}
}
}
} catch(Exception e) {
Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();
}
}
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();
}
}
我知道错误在我的java代码中,谁能帮助我并告诉我应该在代码中更改什么,谢谢