2013-07-31 20 views
1

您好我是新來的Android和我試圖上傳數據到本地主機,但我沒有得到任何響應或錯誤瞭如何在Android應用

public void postData(String valueIWantToSend) { 
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://10.0.2.2/text.php"); 

try { 
// Add your data 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend)); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
// TODO Auto-generated catch block 
} catch (IOException e) { 
// TODO Auto-generated catch block 
} 

請建議我我如何可以打印響應響應這樣我能來知道哪裏是問題

+0

什麼你正在試圖做的?通過PHP連接到MySQL? – Goofy

+0

因此是我想要做的 http://stackoverflow.com/questions/17962183/post-data-to-localhost-in-android-app/17962220?noredirect=1#comment26254256_17962220 –

回答

0

從你的迴應:

StringBuilder builder = new StringBuilder(); 
int statusCode = response.getStatusCode(); 
    if (statusCode == 200) { 
    HttpEntity entity = response.getEntity(); 
    InputStream content = entity.getContent(); 
    BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
    String line; 
    while ((line = reader.readLine()) != null) { 
     builder.append(line); 
    } 
    }