How to get Web content from web server to android application - android

I am doing a project in android, to get the website data's into my android application.
I tried JSON but it doesn't help me to achieve my project. Please help me...

i've used this code for my project
HttpClient client = new DefaultHttpClient();
String getURL = "http://yourservername/wifi.ashx?cmd=get;1;";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
String str;
if (resEntityGet != null) {
str = EntityUtils.toString(resEntityGet).substring(2, 4); // determined the maximumum id in server
str.lastIndexOf(str);
}
} catch (Exception e) {
e.printStackTrace();
// link.setText("errorororo");
}

Related

Foursquare API 404 error: Endpoint not found

I am trying to do a GET request using the foursquare checkin endpoint. I'm getting back a 404 error which is endpoint not found. Any help with why that could be happening would be great!
try {
URI url = new URI("https://api.foursquare.com/v2/user/self/checkins?oauth_token="+TokenStore.get().getToken()+"&v=20140219");
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
is = entity.getContent();
int responceCode = response.getStatusLine().getStatusCode();
Log.i(TAG, "Responce = "+ responceCode);
} catch(Exception e) {
e.printStackTrace();
}
The endpoint is users/self, not user/self, which you have. Just a typo :)

UnknownHostException in version 4.1.2 in Android

I have an HTTP get Service url and i can connect and get resource from this url on almost all version of sdk but in 4.1.2 it gives UnknownHostException. Internet permission is already there and also internet connection is active. Please help.
Here is code:
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("myhosturl");
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null)
{
String response = EntityUtils.toString(resEntityGet);
}
}
catch (Exception e)
{
e.printStackTrace();
}
Actually this url is called when there is any incoming call that time it gives exception. If i hit this url any other time it works.

Error using HTTP Post

I got an error using HttpPost for sending an MMS in Android.
In the Logcat it says:
ERROR/Here(447): ---------Error-----Target host must not be null, or set in parameters.
My sample code:
String url = "myurl";
HttpClient httpClient = new DefaultHttpClient();
try {
httpClient.getParams().setParameter(url, new Integer(90000)); // 90 second
HttpPost post = new HttpPost(url);
File SDCard = Environment.getExternalStorageDirectory();
File file = new File(SDCard, "1.png");
FileEntity entity;
entity = new FileEntity(file,"binary/octet-stream");
entity.setChunked(true);
post.setEntity(entity);
post.addHeader("Header", "UniqueName");
Log.i("MMSHTTP","----post---------------"+post);
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e("Here",
"--------Error--------Response Status line code:" + response.getStatusLine());
}
else
{
// Here every thing is fine.
}
HttpEntity resEntity = response.getEntity();
if (resEntity == null) {
Log.e("Here","---------Error No Response!!-----");
}
} catch (Exception ex) {
Log.e("Here","---------Error-----"+ex.getMessage());
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
How do I fix the error?
The url you're specifying is in your sample code is:
String url = "myurl";
In order for HttpClient to be able to determine the host name, you're going to need to supply a valid url. Something along the lines of:
String url = "http://myurl.com/index";
Note: The 'http://' is important so that the appropriate protocol can be determined.
This guy had the same problem.

Android - image upload sending no content

I've been looking into this for the last day or two and can not seem to find a solution to my issue. I am trying to post an image to a server using httppost.
I have tried two ways of doing this and both complete the post but with no content i.e. the content length is 0.
The first is as follows:
String url = "MYURL";
HttpClient httpClient = new DefaultHttpClient();
try {
httpClient.getParams().setParameter("http.socket.timeout", new Integer(90000)); // 90 second
HttpPost post = new HttpPost(url);
File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(SDCardRoot,"/DCIM/100MSDCF/DSC00004.jpg");
FileEntity entity;
entity = new FileEntity(file,"binary/octet-stream");
entity.setChunked(true);
post.setEntity(entity);
post.addHeader("Header", "UniqueName");
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e("Here","--------Error--------Response Status line code:"+response.getStatusLine());
}else {
// Here every thing is fine.
}
HttpEntity resEntity = response.getEntity();
if (resEntity == null) {
Log.e("Here","---------Error No Response!!-----");
}
} catch (Exception ex) {
Log.e("Here","---------Error-----"+ex.getMessage());
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
and the second is:
String url = "MYURL";
//File SDCardRoot = Environment.getExternalStorageDirectory();
File file = new File(Environment.getExternalStorageDirectory(),"/DCIM/100MSDCF/DSC00004.jpg");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true);
// Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
Log.d("finishing", "The try catch function");
} catch (Exception e) {
// show error
}*/
As you can see I have hardcoded a path to a specific image, this is to be dynamic when I get it up and running.
Can anyone see what i'm doing wrong? Am I leaving out something? I know I use setChunked and setContenttype - is there a setContent option?
Any help would be grately appreciated.
Thanks,
jr83.
You can use upload your image by sending multipart messages; you might find this discussion useful.

Parsing the XML Response in an Android Application

I want to Parse the XML Response in my Application using a SAX PArser, I don't know how to do that, So Can anybody please giude me to the right path.
An example with a little coding or a link will be OK.
Thanks,
david
try {
HttpClient client = new DefaultHttpClient();
String getURL = <URL>;
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
mResEntityGet = responseGet.getEntity();
if (mResEntityGet != null) {
//do something with the response
content = EntityUtils.toString(mResEntityGet);
}
} catch (Exception e) {
e.printStackTrace();
}
"content" will be the string of XML format, parse it using XML pull parser.

Categories

Resources