2014-04-02 20 views
0

我用JSON從android發送數據到php。爲此,我使用的代碼如下我的代碼不能用於發送數據到php

  HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://futuretime.in/reg.php"); 

      httppost.setHeader("content-type", "application/json"); 

      JSONObject dataJson = new JSONObject(); 


      dataJson.put("password", password); 
      dataJson.put("number", Integer.parseInt("5556")); 

      StringEntity entity = new StringEntity(dataJson.toString()); 
      httppost.setEntity(entity); 
      HttpResponse response = httpclient.execute(httppost); 
      text=response.toString(); 

當我excuted這我得到一個消息,如:org.apache.http.message.basichttpresponse 4fb06e5e

回答

0

您應該致電response.getEntity().getContent()

0

試試這個只是通過你的網址在這種方法

public void getUrlData(String url) 
{ 
    String result=""; 

    //Making HTTP request 
    try 
    { 
     HttpParams httpParameters = new BasicHttpParams(); 
     HttpConnectionParams.setConnectionTimeout(httpParameters, 30000); 
     HttpConnectionParams.setSoTimeout(httpParameters, 30000); 

     //defaultHttpClient 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     //httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     inputStream = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line); 
     } 
     inputStream.close(); 
     result = sb.toString(); 

    } 
    catch (Exception e) 
    { 
     url_error = 1; 
     System.out.println(e.toString()); 
    } 
} 

而且事後檢索字符串結果你的數據,你可以把裏面的日誌導致已知數據

+0

我應該如何傳遞數據 – user3481084

+0

您需要追加你的數據到網址,在你的情況下你的發送密碼和數字,所以你應該這樣發送 String basicUrl =「http://futuretime.in/reg.php」; String url = basicUrl +「?<你的php文件中的密碼變量名> =」+ <你的密碼> +「&<你的php文件中的變量名> =」+ ; getUrlData(url); –