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
Related
In my android application I try to send a json object to a distant server, when I run it I get an error in httpclient.execute(httpPost)
This is a part of my code.
public static String GET(String url , JSONObject js){
try {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Authorization", "Basic **********");
httpPost.setEntity(new StringEntity(js.toString()));
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(httpPost);
} catch (Exception e) {
Log.i("Console", "Error");
}
Any help please.
I guess your Problem is, that you try to run your Network Request from your Main Thread.
I would discourage you to use the Apache HTTP Client at all.
It became deprecated for Marshmallow, see here
Maybe try OkHttp. It offers you the possibility to run a request asynchronously.
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";
}
}
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
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();
}
}
};
I'm making an http connection using following code:
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response = client.execute(httpPost);
I want to read a bytearray from the response object.
How would I do that?
You can read the byte array directly with the method: EntityUtils.toByteArray
Example
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("localhost:8080/myurl");
HttpResponse response = client.execute(get);
byte[] content = EntityUtils.toByteArray(response.getEntity());
You can use:
HttpResponse response = client.execute(httpPost);
String content = EntityUtils.toString(response.getEntity());
byte[] bytes = content.getBytes("UTF8");
You can replace the character encoding with one appropriate for the response.
I did something very similar to this, but it was a while ago.
Looking at my source code I used
HttpClient client = new DefaultHttpClient();
String url = "http://192.168.1.69:8888/sdroidmarshal";
HttpGet getRequest = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String proto = client.execute(getRequest, responseHandler);
I'm pretty certain that the ResponseHandler is the key to this. My get request simply returned something I needed as a string, which was quite simple.
In the case of a bytearray you'll probably want to use an InputStream like so
ResponseHandler<InputStream> responseHandler = new BasicResponseHandler();
InputStream in = client.execute(getRequest, responseHandler);
After which just handle the InputStream as normal.
A bit of googling suggested you may also need to use HttpResponseHandler() rather than BasicResponseHandler() as in my example, I'd fiddle.
The full source code of my work is here, it might be helpful for you.
Kotlin Way
run this on async task on background thread otherwise it will through the error that network task is running on main thread
doAsync {
val client: HttpClient = DefaultHttpClient()
val get = HttpPost("*your url*")
get.setHeader("Authorization","Bearer "+Utils.token)
val response: HttpResponse = client.execute(get)
val content = EntityUtils.toByteArray(response.getEntity())
onComplete {
createPdf(content)
}
}