0

This question is a duplicate for sure but I need an answer and can't find it.

I need to send a Json json object to the server and retrieve another json. What I've tried so far isn't working, nothing happens.

What am I doing wrong?

The json object to send is userCreate and the address is path, tell me if you really need the adress and json format.

Solved, FINAL CODE:

Thread x = new Thread(){
                        public void run(){
                            Looper.prepare();
                            DefaultHttpClient client = new DefaultHttpClient();
                            HttpPost post = new HttpPost(path);
                            String result = "";
                            try {        
                                post.setEntity(new StringEntity(userCreate.toString())); 
                                HttpResponse response = client.execute(post);                               
                                HttpEntity entity = response.getEntity();
                                result = EntityUtils.toString(entity);
                                Toast.makeText(ctx,"Result: " + result + " response: " + response.getStatusLine().getStatusCode(), 10000).show();             
                            } catch(Exception e) {
                                e.printStackTrace();
                            }
                            Looper.loop();
                        }
                    };
                    x.start();
4

4 回答 4

2

Try:

DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(request.toString(), "utf-8")); 
                                // request is the JSONObject
HttpResponse response = client.execute(post);
于 2013-09-18T08:47:29.257 回答
1

This works for me :

//MainActivity.java
Pass the name-value pair to server .you can get those values using json-array in PHP.


String registerURL="http:10.0.2.2/register.php";

public void registerUser(String email, String password, String mobile)
 {

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", register_tag));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));
    params.add(new BasicNameValuePair("mobile", mobile));



 JsonParser.makeHttpRequest(registerURL,"POST",params);




}

//JSONParser.java

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET method
public static JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {


    // Making HTTP request
    try {

        // check for request method
        if (method == "POST") {


            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

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



        } else if (method == "GET") {


            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }

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


    try {

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();


    } catch (Exception e) {

    }

    // try parse the string to a JSON object
    try {

        jObj = new JSONObject(json);

    } catch (JSONException e) {

    }

    // return JSON String
    return jObj;

}
}
于 2013-09-18T09:41:58.637 回答
0

Try something like:

DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(path);
post.setEntity(new StringEntity(request.toString(), "utf-8"));
HttpResponse response = client.execute(post);

Hop this will help.

于 2013-09-18T08:54:22.327 回答
0

This works for me:

// General query of the website. Takes an object of type Q and returns one of class R.
public static <Q extends JSONObject, R> R query(String urlBase, String op, Q q, Class<R> r) throws IOException {
  // Prepare to post.
  final HttpPost postRequest = new HttpPost(urlBase + op);

  if (q != null) {
    // Get it all into a JSON string.
    final StringEntity input = new StringEntity(asJSONString(q));
    input.setContentType("application/json");
    postRequest.setEntity(input);
  }
  log.debug("> " + urlBase + op + (q == null ? "" : " " + q));
  // Post it and wait.
  return readResponse(postRequest, HttpClientPool.getClient().execute(postRequest), r);
}
于 2013-09-18T09:16:28.197 回答