1

我正在做一个安卓项目。我是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
}

提前感谢您的帮助。

4

3 回答 3

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:

httpurl连接

于 2012-06-01T08:11:22.250 回答
1

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

    <uses-permission android:name="android.permission.INTERNET" />
于 2012-06-01T09:31:07.257 回答
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!

于 2012-06-01T19:00:20.140 回答