1

I want to access a parameterized url with Android. All it has to do is "load" the page so that it does what its supposed to do (update database with parameters given).

I was having trouble just loading the url so I watched a video on regular HttpClient activity -- just waiting for the response and gathering that information. I figured that it would still be loading the page and therefore also letting that page execute. I can't even get that to run the page correctly or gather a response.

Here's the code I used:

String url = "http://www.removed.com?param1="+param1+"&param2="+param2;

        try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            InputStream webs = entity.getContent();
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"), 8 );
                test.setText(reader.readLine());
                webs.close();
            }catch(Exception e) {
                Log.e("Error in conversion: ", e.toString());
            }
        }catch(Exception e) {
            Log.e("Error in connection: ", e.toString());
        }

Please let me know what I can do to get this to execute the page and update the database. If I put in the parameters manually into a browser, it works.

4

2 回答 2

2

You haven't posted where you're running this or what the error is, but the first two things that come to mind are:

  • Do you have INTERNET permissions set in the manifest?
  • If this is Honeycomb, is this running in a separate thread? - As of 3.0 you can't run HTTP requests in the main display thread.
于 2012-05-26T07:25:48.857 回答
0

After reading your comments in answers I think U are searching for response code. I am posting you code here which is working well in my code

String urlGET = "http://www.removed.com?param1="+param1+"&param2="+param2;

HttpGet getMethod = new HttpGet(urlGET);
    // if you are having some headers in your URL uncomment below line 
    //getMethod.addHeader("Content-Type", "application/form-data");
    HttpResponse response = null;
    HttpClient httpClient = new DefaultHttpClient();
    try {
        response = httpClient.execute(getMethod);

        int responseCode = response.getStatusLine().getStatusCode();

        HttpEntity entity = response.getEntity();
        String responseBody = null;

        if (entity != null) {
            responseBody = EntityUtils.toString(entity);
          //here you get response returned from your server
            Log.e("response = ", responseBody);

            // response.getEntity().consumeContent();

        }
        JSONObject jsonObject = new JSONObject(responseBody);
         // do whatever you want to do with your json reponse data

        }
        catch(Exception e)
        {
        e.printStackTrace();
        }
于 2014-09-26T10:26:52.333 回答