2012-06-18 52 views
1

我無法從服務器檢索數據。我不知道在那裏我已經在code..When出了錯我運行應用程序時,不會顯示在模擬器上。該代碼如下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"); 
} 

給予}

+1

當你運行這段代碼時會發生什麼? –

+0

什麼都沒有顯示..因爲它沒有連接到服務器 – Tina

回答

1

從服務器的數據我認爲錯誤是在

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

    json.put("position", "sysdev"); 

    JSONArray postjson = new JSONArray();