Hi我有php代码来编码数组,我希望我的android应用程序接收数组并存储到本地变量
服务器端的php代码:
echo json_encode(array('result'=>$result));
如何让android从服务器接收数组。我正在考虑使用Volley库,但不知道如何实现
应该是这样的:
ArrayList eventdetail = repond(url...)
我相信你需要时间来研究。看起来你才刚刚开始。
他们有很多方法可以做到这一点。
您可以随意使用Rest API或SOAP API。或者你可以在android上建立自己的http客户端,从web服务器请求和接收数据。
一切都好。
对于SOAP,您可以使用Ksoap2
对于实现Volley来说,这是最好的来源。
使用以下代码解析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;
}
}