繁体   English   中英

我的代码无法将数据发送到php

[英]my code not working for sending data to php

我使用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

尝试这个

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

参考: EntityUtils Android

你应该打电话给response.getEntity().getContent()

试试这个只是通过这种方法传递您的网址

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结果中检索数据,您可以将结果放入日志中以获取已知数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM