如何将php json_encode($array)导入android以操作it数据?

如何将php json_encode($array)导入android以操作it数据?,php,android,json,Php,Android,Json,如何将phpjson\u encode($array)导入android以操作it数据 我有一个用json_encode($array)编码数组的php;当我 echo json_encode($array); 我得到: [{“id”:“1”,“name”:“player1”,“score”:“20”,“quarter”:“Q-1”},{“id”:“2”,“name”:“player2”,“score”:“18”,“quarter”:“http:\/\/localhost\/win.jpg”}

如何将php
json\u encode($array)
导入android以操作it数据

我有一个用json_encode($array)编码数组的php;当我

echo json_encode($array);
我得到:

[{“id”:“1”,“name”:“player1”,“score”:“20”,“quarter”:“Q-1”},{“id”:“2”,“name”:“player2”,“score”:“18”,“quarter”:“http:\/\/localhost\/win.jpg”}]

现在在android中,我想从php中获取该数组,并将其放入一个数组中,让我以索引0为例,其中包含键名和字符串值,然后将该字符串设置为textView文本。在iphone中,我只执行以下代码:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
(数据是我的本地url“http://localhost/my.php))

然后,我可以使用
valueForKey:name
objectAtIndex:0
轻松地从字典中获取数据,并将其字符串放到文本字段中

请用java完成代码实现,以便我能理解。因为我是java新手,我用很多错误和时间试图用不同的方式来做这件事,这让我不知所措


多亏了解决我问题的那个。

使用org.apache.http.client.HttpClient接收对字符串的响应

HttpClient httpclient = new DefaultHttpClient();
try {
    HttpPost httppost = new HttpPost(this.getURL());
    // attach the request with post data
    List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 
    pairs.add(new BasicNameValuePair("username", username)); 
    pairs.add(new BasicNameValuePair("password", password));        
    httppost.setEntity(new UrlEncodedFormEntity(pairs));
    //send request to server
    HttpResponse response = httpclient.execute(httppost);
    //trace response
    InputStream is = response.getEntity().getContent();
    //convert response to string
    BufferedReader myReader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
    StringBuilder sb = new StringBuilder();
    sb.append(myReader.readLine() + "\n");
    String line="";
    while ((line = myReader.readLine()) != null) {
        sb.append(line + "\n");
    }
    result = sb.toString();
} catch (Exception e) {
    e.printStackTrace();
}

有许多第三方类可以自动生成大部分作业。我们不需要发明轮子。轻松使用改装或截击。在我的项目中有很多工作做得很好。他们代表你处理很多事情。 定义一个类,将每个玩家对象的所有属性打包到单个对象中,然后使用改装API: 也许以下提示会对您有所帮助:

public interface QuestionAPI {
    @GET("player.php")//you can call php file directly
    public void getFeed(Callback <List<PlayerObject>> playerobject);
}
公共接口API{
@GET(“player.php”)//您可以直接调用php文件
public void getFeed(回调playerobject);
}

发生此错误是因为您发送的json数组没有名称

存储json数组后,请执行以下操作:

echo json_encode(array('acdata'=>$acdata));



eg:
while($row = $stmt->fetch ()){
 $acdata[] = array(
     'acid' => $acid,
     'address' => $address,
     'companyname' => $companyname,
     'dateofpurchase' => $dateofpurchase,
     'ac_type' => $ac_type
);
}
header('Content-type: application/json');
echo json_encode(array('acdata'=>$acdata));
然后在android中通过以下方式检索:

JSONObject jsonRootObject = new JSONObject(result);
JSONArray jsonArray = jsonRootObject.optJSONArray("acdata");
JSONObject jsonObject = jsonArray.getJSONObject(1);
name = jsonObject.optString("acid");
//acid是json中键的名称


//将其放入循环以获取所有值

如果您能解释如何使用org.apache.http.client.HttpClient接收对字符串的响应,我将不胜感激,结果就是响应字符串?@coderican我个人使用它,它对我来说很好,并且隐藏了
org.apache.http.client.HttpClient
的复杂性
org.apache.http.client.HttpClient
已经被弃用了一段时间,在Android 6中不再可访问。改用HttpUrlConnection,或者使用OkHttp、改型、Volley等库。
JSONObject jsonRootObject = new JSONObject(result);
JSONArray jsonArray = jsonRootObject.optJSONArray("acdata");
JSONObject jsonObject = jsonArray.getJSONObject(1);
name = jsonObject.optString("acid");