我的代码不用于向php发送数据

时间:2014-04-02 09:22:11

标签: java php android http

我使用JSON从android向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();

当我执行此操作时,我收到的消息如下:org.apache.http.message.basichttpresponse 4fb06e5e

3 个答案:

答案 0 :(得分:0)

试试这个

text = EntityUtils.toString(response.getEntity());

参考: EntityUtils Android

答案 1 :(得分:0)

你应该致电 - response.getEntity().getContent()

答案 2 :(得分: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());
    }
}

然后从String结果中检索您的数据,您可以将结果放入日志中以知道数据