如何从我的Android项目发送HTTP Post请求到谷歌应用程序引擎?

时间:2012-06-01 07:54:39

标签: android google-app-engine http httprequest http-request

我正在开发一个Android项目。我是android编程的新手。如何从我的项目向谷歌应用引擎发送HTTP发布请求?我搜索并发现此代码用于从android发送请求,但它无法正常工作。以下是我正在使用的代码:

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
}

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

这是我在java中用于http请求的类:

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

如果想了解更多,请访问此CW:

httpurlconnection

答案 1 :(得分:1)

我没有允许我的应用用户使用互联网。为此,我们只需要在AndroidManifest.xml中添加此行。

    <uses-permission android:name="android.permission.INTERNET" />

答案 2 :(得分:0)

在app引擎上使用restlet(http://wiki.restlet.org/docs_2.0/13-restlet/21-restlet.html)来处理http请求进来。这通常不是app的方式使用引擎,但它可以满足您的需求。

在andoid中,只需在相应的网址上执行正常的http请求(http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java)。

您应该能够从restlet教程中找出如何设置url。部署到应用引擎后,网址将类似于http://myapp.appspot.com/goodstuff

祝你好运!

相关问题