unknown host exception from android to GAE
I am trying to post from android to GAE, I am getting unknown host exception. Here is my code,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://silver-kites.appspot.com");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("abc", "123"));
nameValuePairs.add(new BasicNameValuePair("xyz", "456"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getE开发者_运维百科ntity();
if(entity != null){
InputStream inputStream = entity.getContent();
ResponseText.setText(convertStreamToString(inputStream));
}
} catch (ClientProtocolException e) {
ResponseText.setText(e.getMessage());
} catch (IOException e) {
ResponseText.setText(e.getMessage());
}
When I am trying to connect to localhost:8888 am getting connection refused. I donno how to go pass thru this. Help me.
I tried that too, still getting Unknown Host exception in android. Do I need to do any setting for accessing HTTP? or in emulators the http wont work?
I set the <uses-permission android:name="android.permission.INTERNET"></uses-permission>
in android manifest xml. Still no hopes.
Any helps? I checked in GAE and my request doesn't reach there.
Here is my code,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://silver-kites.appspot.com/silverscores");
try {
nameValuePairs.add(new BasicNameValuePair("name", "JP"));
nameValuePairs.add(new BasicNameValuePair("seconds", "123"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream inputStream = entity.getContent();
ResponseText.setText(convertStreamToString(inputStream));
}
} catch (ClientProtocolException e) {
ResponseText.setText(e.getMessage());
} catch (IOException e) {
ResponseText.setText(e.getMessage());
}
I've hot exaclty the same problem.
After some googling and stackoverflowing :) I found solution:
Check if you emulator has network connection (network icon in notification bar) -- my emulator didn't has because firewall blocked it :(. I found this tip here.
Check if you set network permission for your app by adding line
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
in application
AndroidManifest.xml
file.
According to Emulator Networking IP 10.0.2.2
should be used instead of localhost
/127.0.0.1
.
localhost
/127.0.0.1
is Android device IP address, not developer machine.
Try with the following (name,value) pair
nameValuePairs.add(new BasicNameValuePair("name", "123"));
nameValuePairs.add(new BasicNameValuePair("seconds", "456"));
Now you won't get invalid parameters exception, but the response for this POST is "Sever Error" , you can check with GAE about this error.
I was doing everything fine. Only one problem was emulator was running in airplane mode, so it is not using internet connection. So I was getting unknown host exception. Now it is working fine. Thanks for your support.
精彩评论