简体   繁体   中英

my code not working for sending data to php

I used JSON for sending data to php from android. For this I used the code as below

          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();

when i excuted this i am getting a message like: org.apache.http.message.basichttpresponse 4fb06e5e

Try this

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

Reference: EntityUtils Android

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

Try this just pass your url in this method

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());
    }
}

And afterwards retrieve your data from String result, you can put result inside log to known the data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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