2012-06-18 2 views
1

서버에서 데이터를 가져올 수 없습니다. 난, 서버에서 데이터를 에뮬레이터 .. 코드가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

에 표시되지 않습니다 내가 응용 프로그램을 실행 code..When에 잘못 갈 어디 확실하지 않다 오류가 있다고 생각합니다

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

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

    JSONArray postjson = new JSONArray(); 
관련 문제