1

I am working on an android project. I am new in android programming. How can i send a HTTP post request from my project to google app engine? I searched and found this code for sending request from android but its not working. Following is the code i am using:

try{
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.example.com/servleturl");

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("username", userEmailStr));
    nameValuePairs.add(new BasicNameValuePair("password", userPasswordStr));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

    info.setText(response.toString());
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}

Thanks for help in advance.

Piscean
  • 3,010
  • 12
  • 43
  • 88
  • Posting to GAE is no different than posting to any Web server. You might get better help if describe how it is 'not working' and provide stack traces if available. BTW, "http://www.runno.me/iphonelogincheck" does not look like a GAE URL, are you posting to the correct endpoint? – Nikolay Elenkov Jun 01 '12 at 08:34
  • Sorry now i edited it. Infact its a servlet path where the request should go. – Piscean Jun 01 '12 at 08:55
  • 1
    So what exactly is the problem? Error on Android side? You get the wrong data? Data never makes it the server? Something else? – Nikolay Elenkov Jun 01 '12 at 09:05
  • Its my first application in android. And i found out what was the problem. I didn't add this line in AndroidManifest.xml. . Thanks for your time Nikolay – Piscean Jun 01 '12 at 09:10
  • OK, cool. Post it as an answer and accept it. – Nikolay Elenkov Jun 01 '12 at 09:20
  • you forgot to accept your answer – Th0rndike Jun 01 '12 at 10:24
  • I tried but they are saying that "You can accept your own answer in 2 days." So i have to wait for 2 days my friend. – Piscean Jun 01 '12 at 11:40

3 Answers3

1

Here's the class that i use for http requests in java:

public class WSConnector {
final String baseUrl = "http://www.myURL.com/"; //this is the base url of your services
String realUrlWithParams="";
String realUrl="";
String params = "";
String charset = "UTF-8";

HttpURLConnection connection = null;

public WSConnector(String serviceName,String params,String charset){ //we create the connector with everything we need (params must be ready as value pairs, serviceName is the name of your service):
    if (charset!=null){
        this.charset = charset;
    }
    this.realUrlWithParams = baseUrl+serviceName+"?"+params;
    this.realUrl = baseUrl+serviceName;
}

public String getResponse(){//getResponse will get your the entire response String
    String result = "";
    System.out.println("trying connection");
    try {
        connection = (HttpURLConnection) new URL(realUrlWithParams).openConnection();
        //connection.setRequestProperty("Accept-Charset", charset);
        int status = connection.getResponseCode();
        System.out.println("status:"+status);
        if (status==HttpURLConnection.HTTP_OK){
            InputStream responseStream = connection.getInputStream();
            BufferedReader reader = null;
            reader = new BufferedReader(new InputStreamReader(responseStream));
            for (String line; (line = reader.readLine()) != null;) {
                System.out.println("line is:" +line); 
                result = result+line;
                System.out.println("result is:"+result);
            }
        }
    } catch (MalformedURLException e) {
            System.out.println("ERROR IN CONNECTOR");
            e.printStackTrace();
    } catch (IOException e) {
            System.out.println("ERROR IN CONNECTOR");
            e.printStackTrace();
    }
    System.out.println("finished connection");
return result;
}

if wanting to know some more, visit this CW:

httpurlconnection

Community
  • 1
  • 1
Th0rndike
  • 3,360
  • 3
  • 19
  • 39
1

I didn't give permission to user of my app to use internet. For doing that we just need to add this line in AndroidManifest.xml.

    <uses-permission android:name="android.permission.INTERNET" />
Piscean
  • 3,010
  • 12
  • 43
  • 88
0

Use restlet (http://wiki.restlet.org/docs_2.0/13-restlet/21-restlet.html) on app engine to handle the http requests as they come in. This isn't typically how app engine is used, but it'll work for what you want.

In andoid, just do a normal http request (http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java) on the appropriate url.

You should be able to figure out how to setup the url from the restlet tutorial. Once you deploy to app engine, the url will be something like http://myapp.appspot.com/goodstuff

Good luck!

Adam
  • 928
  • 9
  • 21