您好我有PHP代码编码数组,我希望我的Android应用程序接收数组并存储到本地变量
服务器端的PHP代码:
echo json_encode(array('result'=>$result));
如何让android从服务器接收数组。我正在考虑使用Volley libray,但不知道如何实现
应该是这样的:
ArrayList eventdetail = repond(url...)
答案 0 :(得分:0)
我相信你需要时间去研究。好像你刚刚开始。
他们有很多方法可以做到这一点。
您可以使用Rest API或SOAP API,无论您感觉如何。 或者您可以在Android上构建自己的http客户端,以便从Web服务器请求和接收数据。
一切顺利。为了实施Volley,这是Ksoap2。
答案 1 :(得分:0)
使用下面的代码来解析json数组
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
JSONArray jArrayVersion = json.getJSONArray("result");
for (int j = 0; j < jArrayVersion.length(); j++) {
JSONObject items = jArrayVersion.getJSONObject(j);
String namesv = items.getString("name");
String message = items.getString("message");
}
// JSONParser类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
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.ClientProtocolException;
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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Making HTTP request
try {
// defaultHttpClient
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
System.out.println("url " + url);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutSocket = 6000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
System.out.println("url " + url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
System.out.println("url0 " + url);
HttpResponse httpResponse = httpclient.execute(httpPost);
System.out.println("url1 " + url);
HttpEntity httpEntity = httpResponse.getEntity();
System.out.println("url2 " + url);
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}