Android HTTP request failing on emulator - android

I am requesting a HTTP request from Android Emulator but its getting failed .. Is there any issue with the emulator?
I have also enabled internet connection permission from Manifest
HttpClient Client = new DefaultHttpClient();
// Create URL string
String URL = "http://androidexample.com/media/webservice/httpget.php?user="+loginValue+"&name="+fnameValue+"&email="+emailValue+"&pass="+passValue;
//Log.i("httpget", URL);
try
{
String SetServerString = "";
// Create Request to server and get response
HttpGet httpget = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
SetServerString = Client.execute(httpget, responseHandler);
// Show response on activity
content.setText(SetServerString);
}
catch(Exception ex)
{
content.setText("Fail!");
}
}
I am using below link to test on emulator
http://androidexample.com/How_To_Make_HTTP_Get_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=63&aaid=88

Related

how to call web service in android

i am new in Android development and now i am developing small application of google map. i have integrated google map,but now i want to show google places on map therefore i want to call google api web service to get locations, but i don't know how to call web service in android.enter code here
enter code here
public void getLocation()
{
HttpClient httpClient = new HttpClient()
AsyncHttpClient client =new AsyncHttpClient();
}
You can call Url's with HttpConnection.
For Example:
public String getValuefromUrl(String url)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
Log.v("PAGE",page);
return page;
}
catch(Exception ex)
{
ex.printStackTrace();
return "zero";
}
}

Http digest authentication on Android fails with 401

I am trying to do http digest authentication from an android client. Here is a curl line that connects and does the digest successfully.
curl -i --digest --user 'c#c.com:500a436e-2d5d-4a2e-be82-19651f7ea904' -v https://localhost:8080/v1/resources/debug
Here is one attempt in my android client that returns a 401.
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("user agent");
String url = "http://localhost:8080/v1/resources/debug";
URL urlObj = new URL(url);
HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort());
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("c#c.com", "500a436e-2d5d-4a2e-be82-19651f7ea904");
CredentialsProvider cp = new BasicCredentialsProvider();
cp.setCredentials(scope, creds);
HttpContext credContext = new BasicHttpContext();
credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp);
HttpGet job = new HttpGet(url);
HttpResponse response = httpClient.execute(host,job,credContext);
StatusLine status = response.getStatusLine();
System.out.println("#### " + status.toString());
httpClient.close();
Here is a second attempt in the client, also returns a 401.
DefaultHttpClient httpclient = new DefaultHttpClient();
DefaultHttpClient httpclient2 = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://localhost:8080/v1/resources/debug");
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine());
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
Header authHeader = response.getFirstHeader(AUTH.WWW_AUTH);
System.out.println("authHeader = " + authHeader);
DigestScheme digestScheme = new DigestScheme();
digestScheme.processChallenge(authHeader);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("c#c.com", "500a436e-2d5d-4a2e-be82-19651f7ea904
httpget.addHeader(digestScheme.authenticate(creds, httpget));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient2.execute(httpget, responseHandler);
System.out.println("responseBody : " + responseBody);
}
Can anyone see what I'm missing to get the authentication to work? We know it's not a server problem since the curl line works great.

Android HTTP POST request sent from application but server "sees" it as a GET request

I am sending some information to a sever for a university project of mine. The problem i am having is that the sever will only acpect POST request, it will not parse GET requests which is fair enough.
The issue i am having is that i am sending a httpPost request i check this using the built in Android method (see below) but when it arrives at the server it sees it as a GET request.
Post code:
JSONObject auth = new JSONObject();
auth.put("TEST", "TESTING");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://xxxxxxxxxxxxx/upload.php");
String meth = httpPost.getMethod();
Toast checker = Toast.makeText(this, meth, Toast.LENGTH_LONG);
checker.show();
String json = "";
json = auth.toString();
StringEntity se = new StringEntity(json);
httpPost.setHeader("User-Agent", "android app");
httpPost.setEntity(se);
httpclient.execute(httpPost);
The check Toast, displays the value POST, which is correct.
The sever log shows this as a GET request.
xxxxxxxxxxx MY IP - - [09/Dec/2013:00:20:57 +0000] "GET /upload.php HTTP/1.1" 405 352 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"
Edited severname / Ip's out of the log and code.
ANy ideas?
Use this method my friend for posting data to server
public Boolean postDataWithURL(String url, String fileUrl,
ArrayList<NameValuePair> listParamsWithValues) {
boolean result = false;
try {
int TIMEOUT_MILLISEC = 100000; // = 10 seconds
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(url);
if (fileUrl.equalsIgnoreCase("")) {
request.setEntity(new UrlEncodedFormEntity(listParamsWithValues));
} else {
// System.out.println("file path "+fileUrl+" with actual path "+file);
}
// request.setEntity(new
// ByteArrayEntity(listParamsWithValues.toString().getBytes("UTF8")));
HttpResponse response = client.execute(request);
String responseString = request(response);
// System.out.println(responseString);
if (responseString.toLowerCase().contains("1")) {
result = true;
} else {
result = false;
}
} catch (Exception e) {
// Some things goes Wrong
e.printStackTrace();
}
return result;
}

httpget android wcf error

HttpClient Client = new DefaultHttpClient();
String URL = "http://192.168.2.22:1099/Service1.svc/test";
try
{
String setServerString = "";
HttpGet httpget = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
setServerString = Client.execute(httpget, responseHandler);
lblStatus.setText(setServerString);
}
catch(Exception ex)
{
lblStatus.setText("Fail!");
}
when we call the url it returns a string and is set to lblStatus.
This code is working fine in v2.3.* but not working in v4.0.
i'm able to get the string in v2.3.* versions but not in v4.0.
Indeed you are getting the NetworkOnMainTreadException. This exception has been introduced with HoneyComb and it is raised when an attempt of run a network operation on the UI Thread is made.
If you want to overcame it you have to use an AsyncTask. Read the android painless threading guide

Execute a HTTP request within an app

I was wondering if I could make it open But NOT open the android browser, I just need it to visit: (pretend this is the ip) http;//91.91.91.91:2228?1, where it will trigger action on my arduino mega. I have tried to get it just to do this with this code
onclick(Intent websiteIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http;//91.9.91.91:?1");
websiteIntent.setData(uri);
startActivity(websiteIntent);)
but I don't know how to get it to do so
A HttpClient will allow you to call an arbitrary URL within your app:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http;//91.9.91.91:?1");
HttpResponse response = client.execute(request);
Don't forget to wrap in a try catch though.
edit:
new Thread(){
public void run(){
try{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http;//91.9.91.91:?1");
HttpResponse response = client.execute(request);
}catch(Exception e){
// Handle the exception
e.printStackTrace();
}
}
};

Categories

Resources