Android 从php文件到java数组的JSON
我知道关于这个主题有一些问题,但我读了它们,并尝试了解决方案,但没有成功: PHP脚本给出这个json数组结果:data[x]=Android 从php文件到java数组的JSON,android,json,android-activity,Android,Json,Android Activity,我知道关于这个主题有一些问题,但我读了它们,并尝试了解决方案,但没有成功: PHP脚本给出这个json数组结果:data[x]= ["alon","62","1.82","22","0","70","0","1"] 这是数据[x]变量 我必须将这个结果转换成Java变量,如姓名、体重、身高等。。但是我不知道怎么 请帮帮我 我的职能: private class LongOperation extends AsyncTask<String, Void,
["alon","62","1.82","22","0","70","0","1"]
这是数据[x]变量
我必须将这个结果转换成Java变量,如姓名、体重、身高等。。但是我不知道怎么
请帮帮我
我的职能:
private class LongOperation extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Error = null;
protected void onPreExecute() {
}
protected Void doInBackground(String... urls) {
try {
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
data[x] = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
Toast.makeText(getApplicationContext(),"error2" , Toast.LENGTH_LONG).show();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
Toast.makeText(getApplicationContext(),"error34" , Toast.LENGTH_LONG).show();
cancel(true);
}
return null;
}
public void onPostExecute(Void unused) {
String name = null,weight = null;
if (Error != null) {
} else {
// here I have to do something with the arrays...
Toast.makeText(getApplicationContext(),"d:" + data[x] + "o:" + name + " : " + weight, Toast.LENGTH_LONG).show();
}
x++;
}
}
为此创建一个模态类
class myModal {
private String name, weight, height, ...;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
//and more getters and setters
}
JSONObject json = new JSONObject(data[x]); // in your sample its a JSONArray but its wrong formatted. make sure you encode it properply with php json_encode(array("data", yourdata))...);
myModal modal = new myModal();
modal.setName(json.getString("name"));
php应该类似于
<?php
$data = array("name" => "myname", "weight" => 20);
print json_encode( $data );
?>
而json在本例中可以用
JSONArray json = new JSONArray(data);
for (int i = 0; i <= json.length();i++){
JSONObject jsonObj = json.getJsonObject(i);
myModal modal = new myModal();
modal.setString(jsonObj.getString("name"));
//and so on
}
确保阅读理解的基础知识我试过你说的。仍然不起作用=[php显示:[数据,{name:alon,weight:62,height:1.82,age:22,weeks:0,target:70,ketzev:0,gender:1}].eclipse希望在代码中添加try和catch:JSONObject json=null;try{json=new JSONObjectdata[x];}catch JSONException e{//TODO自动生成的catch块e.printStackTrace;}myModal modal=new myModal;尝试{modal.setNamejson.getStringname;}catch JSONException e{//TODO自动生成的catch块e.printStackTrace;}php文件:$data=arrayname=>$name,weight=>$weight,height=>$height,age=>$age,weeks=>$weeks,target=>$target,ketzev=>$ketzev,gender=>$gender;$length=count$array;echo json_encodearraydata,$data;