PHP 服务器未使用 JSON 对象进行连接



我无法从服务器检索数据。我不确定我在代码中哪里出错了..当我运行应用程序时,来自服务器的数据不会显示在模拟器上。代码如下

private void postData1(){

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
        "http://www.xxxxx.co.uk/NottTest/post.php");
JSONObject json = new JSONObject();
try {
    // JSON data:
    try {
        json.put("name", "Fahmi Rahman");
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    json.put("position", "sysdev");
    JSONArray postjson = new JSONArray();
    postjson.put(json);
    // Post the data:
    httppost.setHeader("json", json.toString());
    httppost.getParams().setParameter("jsonpost", postjson);
    // Execute HTTP Post Request
    System.out.print(json);
    HttpResponse response = httpclient.execute(httppost);
    tv.setText("Hiii");
    // for JSON:
    if (response != null)
    {
        Log.i("Json","respose");
        System.out.print("loooooooooool");

        InputStream is = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        text = sb.toString();
    }
    else{
        tv.setText("no respose");
        Log.i("Noting","happended");
    }
    //tv.setText(text);
} catch (ClientProtocolException e) {
    Log.i("Error","Prtocol");

} catch (IOException e) {
    Log.i("Error","IO");
} catch (JSONException e) {
    Log.i("Error","JOSON");
}

}

我认为错误在

try {
        json.put("name", "Fahmi Rahman");
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    json.put("position", "sysdev");
    JSONArray postjson = new JSONArray();

最新更新