2014-04-02 22 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); –