2012-06-01 38 views
1

我正在開發一個android項目。我是android編程新手。我怎樣才能從我的項目向谷歌應用程序引擎發送HTTP POST請求?我搜索並發現這個代碼發送來自android的請求,但它不工作。以下是我正在使用的代碼:如何從我的android項目向Google應用引擎發送HTTP Post請求?

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 
} 

感謝您提前幫助。

+0

發佈到GAE與發佈到任何Web服務器沒有區別。如果描述「不工作」並提供堆棧跟蹤(如果可用),可能會得到更好的幫助。順便說一句,「http://www.runno.me/iphonelogincheck」看起來不像GAE網址,你是否發佈到正確的端點? –

+0

對不起,現在我編輯它。事實上,它是一個請求應該發送到的servlet路徑。 – Piscean

+1

那麼究竟是什麼問題? Android端的錯誤?你得到錯誤的數據?數據永遠不會成爲服務器?還有別的嗎? –

回答

1

我沒有賦予權限我的應用程序的用戶使用互聯網。爲此,我們只需要在AndroidManifest.xml中添加這一行。在App Engine上

<uses-permission android:name="android.permission.INTERNET" /> 
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

0

使用的Restlet(http://wiki.restlet.org/docs_2.0/13-restlet/21-restlet.html)來處理HTTP請求,因爲他們進來,這ISN」通常應用引擎是如何使用的,但它可以滿足你的需求。

在andoid中,只需在相應的url上執行正常的http請求(http://stackoverflow.com/questions/1359689/how-to-send-http-request-in-java)。

你應該能夠弄清楚如何從restlet教程中設置url。一旦你部署到應用引擎,網址將是類似http://myapp.appspot.com/goodstuff

祝你好運!

相關問題