我无法弄清楚这段代码有什么问题,它应该从我创建的php页面获取数据。 我在Eclipse SDK中使用Eclipse。
package com.myproject.myproject2;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class EntList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entlist);
JSONArray jsonArray = null;
String jsonString = null;
StringBuilder stringBuilder = null;
InputStream inStream = null;
TextView tv = (TextView) findViewById(R.id.listtitle);
ArrayList<NameValuePair> nVPArray = new ArrayList<NameValuePair>(); //This is empty because you're asking for data
try{
//Connect to your script, and save get an object to read the data (inStream)
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://sawtaljabal.com/ar/android_connect/test.php"); // Be sure to replace with your actual script
post.setEntity(new UrlEncodedFormEntity(nVPArray));
HttpResponse postResponse = client.execute(post);
HttpEntity responseEntity = postResponse.getEntity();
inStream = responseEntity.getContent();
}catch(Exception e){
Log.e("Error connecting", e.getLocalizedMessage());
}
try{
//read the stream to a single JSON string
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"iso-8859-1"), 10); // iso-8859-1 is the character converter
stringBuilder = new StringBuilder();
stringBuilder.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
inStream.close();
jsonString = stringBuilder.toString();
}catch(Exception e){
Log.e("Error creating JSON string", e.getLocalizedMessage());
}
try{
//Turn the JSON string into an array of JSON objects
jsonArray = new JSONArray(jsonString);
JSONObject jsonObject = null;
for(int i=0;i< jsonArray.length();i++){
jsonObject = jsonArray.getJSONObject(i);
//do something with the object, like String data = jsonObject.getString("KEY");
String data = jsonObject.getString("n_t");
tv.setText(data);
}
}
catch(JSONException e){
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
我甚至不知道该尝试什么,请帮助我,这是我的第一份申请表。
答案 0 :(得分:0)
如果这是你的第一个Android应用程序,你可能想尝试克隆这个项目并在那里学习代码:
https://github.com/jgilfelt/android-jsonarrayadapter?files=1
希望它有所帮助。
答案 1 :(得分:0)
您确定,您在回复中获得的字符串是JSON Array
。我认为它应该是JSON object
,您可以通过JSON Array
获得更多 // try parse the string to a JSON object
try {
jObj = new JSONObject(jsonString);
} catch (JSONException e) {
}
。
表示
{{1}}
然后从JSON Object解析JSON数组。
结帐this链接,可能会有所帮助。
也可以在AsynTask或其他线程中进行所有网络调用。
答案 2 :(得分:0)
试试我的代码, 它是Bettor使用ResponseHandler它会直接将json响应转换为String,你现在将免于输入流,之后你的代码就好了,祝你好运... :)
For efficient use of this code:
请创建一个asy任务类,并将此代码放入doinbackbrd方法
try {
ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>(2);
nvp.add(new BasicNameValuePair("key1", "value1"));
nvp.add(new BasicNameValuePair("key2", "value2"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = newHttpPost("http://sawtaljabal.com/ar/android_connect/test.php");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
// Log.i("response", responseBody);
} catch (Exception e) {
Log.i("error1", "" + e.toString());
}
答案 3 :(得分:0)
使用php
连接Android
首先,您必须调用一个Web服务(可能是返回php
的{{1}}页面),对此页面的调用应该在json
类中。
然后,您需要以正确的方式解析AsynTask
。
您可以尝试任何好的教程。试试this