HTTP GET in async task does not work - android

I get a response from a HTTPPOST, take a substring from it which is URL and then do a HTTPGET on it, I am unable to get any response. Any inputs on what the issue could be?
String pkmspogout1 = result.substring(result.indexOf(",")+1,result.length());
Log.i("Lpkmspogout1 ", pkmspogout1);
HttpClient httpclient4 = new DefaultHttpClient();
HttpResponse logoutresponse ;
try {
HttpGet httpget = new HttpGet(pkmspogout1);
logoutresponse = httpclient4.execute(httpget);
BufferedReader in2 = new BufferedReader(new InputStreamReader(logoutresponse.getEntity().getContent()));
StringBuffer sb2 = new StringBuffer("");
String line2 = "";
String NL2 = System.getProperty("line.separator");
while ((line2 = in2.readLine()) != null) {
sb2.append(line + NL2);
}
in2.close();
String pkmslogout5 = sb2.toString();
Log.i("logoutresponse from pkms", pkmslogout5+"");
} catch (Exception e) {}

Is pkmspogout1 a correct url?
Also you should make sure to do the following to see any errors:
catch (Exception e) {e.printStackTrace();}
Try encoding the url as follows:
String[] url = pkmspogout1.split("com"); //Or whatever is the last part of the main url.
//The letter "." means any character btw as it is a regular expression.
String finalUrl = url[0];
for(int i=0; i<url.length; i++){
finalUrl += URLEncoder.encode(url[i],"UTF-8");
}
Slightly crude to be honest.... Why when forming on server side would be better to encode it then.
You could just try:
pkmspogout1 = pkmspogout1.replace("http://","");
pkmspogout1 = URLEncoder.encode(pkmspogout1);
pkmspogout1 = "http://"+pkmspogout1;
See if it is only the "http://" that is ruining your url. I still assume that it is the url that is the issue more than anything else as you told me hardcoded it works fine.

It is working finally, Phew !I tried (pkmspogout1.trim()) and it worked ! (spaces before and after the URL were the issue).

Related

Android Http Response is incomplete. Returns an unterminated json object

I am using HttpClient 4.3.6 to perform http GET and POST requests. Right now I am using multipartentity to send a few string parameters and an image in the form of a file. I am able to successfully post the data but my problem comes in when I get the HTTP response. The response contains json data.
What happens is the HTTP response is incomplete and when i try to create a json object with the data i get jsonexception error saying:
Unterminated object at character 407.
I noticed that the response does not contain closed braces. Is this a problem on android or should I check the server? Because I am able to see the data properly on postman and on ios. I have never faced this issue before and don't know how to solve this.
This is my code to post and get the response:
#Override
protected String doInBackground(String... params) {
try {
String url = params[0];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(imageBytes, "image.jpg");
entity.addPart("image_data", bab);
entity.addPart("action", new StringBody("1", "text/plain", Charset.forName("UTF-8")));
entity.addPart("name", new StringBody("asdfg", "text/plain", Charset.forName("UTF-8")));
entity.addPart("user_id", new StringBody("157", "text/plain", Charset.forName("UTF-8")));
entity.addPart("birthday", new StringBody("18-04-1995", "text/plain", Charset.forName("UTF-8")));
entity.addPart("gender", new StringBody("male", "text/plain", Charset.forName("UTF-8")));
entity.addPart("is_jlpt_student", new StringBody(String.valueOf(0), "text/plain", Charset.forName("UTF-8")));
entity.addPart("relationship", new StringBody("Father", "text/plain", Charset.forName("UTF-8")));
entity.addPart("relationship_id", new StringBody(String.valueOf(10002), "text/plain", Charset.forName("UTF-8")));
entity.addPart("is_creator", new StringBody(String.valueOf(1), "text/plain", Charset.forName("UTF-8")));
entity.addPart("email", new StringBody(email, "text/plain", Charset.forName("UTF-8")));
httppost.setEntity(entity);
HttpResponse resp = httpclient.execute(httppost);
String response = EntityUtils.toString(resp.getEntity());
Log.i("HttpResponse", response);
return response;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute (String result) {
super.onPostExecute(result);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(result);
JSONObject json_data = jsonObject.getJSONObject("data");
String json_userid = json_data.getString("user_id");
String json_username = json_data.getString("name");
String json_email = json_data.getString("email");
String json_country = json_data.getString("country_code");
String json_imagefilename = json_data.getString("image_filename");
String json_imgurl = json_data.getString("image_url");
Toast.makeText(ParentGuardianProfile.this, "ImageFile " + json_imagefilename, Toast.LENGTH_SHORT).show();
User new_user = userdao.createUser(json_userid, json_username, json_email,json_imagefilename,json_country,selectedImageUri.toString(), 1);
Log.i("SQLITE", "added user : " + new_user.getmUserName() + new_user.getmId());
} catch (JSONException e) {
e.printStackTrace();
}
}
And my json response is :
{"status":1,"message":"success","data":{"child_id":"381","name":"asdfg","image_filename":"C201603021734476.jpg","image_url":"https:\/\/innokid.blob.core.windows.net\/media\/child\/381.jpg","birthday":"18-04-1995","gender":"male","is_jltp_student":"0","relationship":"Father","relationship_id":"10002","is_creator":1,"rank":1,"qrcode_url":"http:\/\/innokid.azurewebsites.net\/uploads\/qrcode\/child_381.png"
I tried using String buffer as suggested in this post String is being truncated when its too long . But i still get the same result.
Code looks ok at first glance.
How do you got know that the json data is cut? Logcat can truncate text. Debugger should be more reliable in this case.
Try to generate this same request with some tools like curl / SoapUI and validate JSON you got with some formatter / validator (you'll easily find a few of such tools).
It's beyond the range of question, but using raw Android built-in communication libraries seems to be a little bit masochistic. Have you ever consider to use Retrofit?
I think this code is problematic String response = EntityUtils.toString(resp.getEntity());
may be you should use some other function to convert response toString...
Apparently the json is missing two curly brackets '}}' at the end, which can happen due to some bug in the toString code.
I pulled up an old project that was using the org.apache.http stuff and below is how I was parsing the response. As you can see it is rather cumbersome. There are many tested and maintained libraries out there that are better suited to this kind of heavy-lifting.
// Get hold of the response entity (-> the data):
HttpEntity entity = response.getEntity();
if (entity != null) {
// Read the content stream
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// Convert content stream to a String
resultString = convertStreamToString(instream);
instream.close();
// Do stuff with resultString here
// Consume Content
entity.consumeContent();
}
And the convertStreamToString() method:
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
* BufferedReader.readLine() method. We iterate until the BufferedReader
* return null which means there's no more data to read. Each line will
* appended to a StringBuilder and returned as String.
*
* (c) public domain:
* http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/
* 11/a-simple-restful-client-at-android/
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
I finally solved this issue by replacing httpclient library with Android Asynchronous Http Client. Now it works fine. Thanks a lot for your help!
However, I still dont understand why the response was truncated when i used httpclient.

Java wont Integer.parseInt(string)

This is my site: http://daniandroid.honor.es/getAllCustomers.php
when you visit the site, you get a simple text "500".
OK.
final ourHTTP hi = new ourHTTP();
out_string = hi.getWebPage("http://daniandroid.honor.es/getAllCustomers.php");
getWebPage returns the string(content).
int veriff = Integer.parseInt(out_string);
if(veriff>1)
{
final_form.setText("ya");
}
final_form is a TextView on my xml file (activity_second.xml)
Application will crash.
ERROR:
at com.android.interval.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid int: "500"
at java.lang.Integer.parse(Integer.java:375)
If I replace the out_string with this
out_string= "500";
everything is good.
My getAllCustomers.php files contains (source):
<?php
echo"500";
?>
Here is also the method to get the content.
public String getWebPage(String adress)
{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet();
InputStream inputStream = null;
String response = null;
try{
URI uri = new URI(adress);
httpGet.setURI(uri);
HttpResponse httpResponse = httpClient.execute(httpGet);
inputStream = httpResponse.getEntity().getContent();
Reader reader = new InputStreamReader(inputStream, "UTF-8");
int inChar;
StringBuffer stringBuffer = new StringBuffer();
while((inChar = reader.read()) != -1){
stringBuffer.append((char)inChar);
}
response = stringBuffer.toString();
}catch(ClientProtocolException e)
{
Log.e(adress, "error");
}catch(IOException e)
{
Log.e(response, "error");
//
}catch(URISyntaxException e)
{
Log.e(response, "error");
}
return response;
}
As discussed in the comments:
I think your HTTP lib passes some non-decimal chars along with the output. Try this:
int veriff = Integer.parseInt(out_string.replaceAll("[\\D]",""));
Where [\\D] is a regex denote any non-decimal character.
May be possible your response contains "" internally in String, Just replace ""
So do it like,
out_string = hi.getWebPage("http://daniandroid.honor.es/getAllCustomers.php");
if(out_string.contains("\"")) // change according to your response, if it contains other charcter
{
out_string = out_string.replace("\"", "")
}
try
{
int veriff = Integer.parseInt(out_string);
if(veriff>1)
{
final_form.setText("ya");
}
}catch(NumberFormatException ex)
{
// Handle exception
}
I would suggest you to handle Exception also, May be if you have other invalid character in your resposnse..
For the version of Java you are running against examine the code.
java.lang.Integer.parse(Integer.java:375)
Crudely using Firebug and examining the response
I see "500" which is certainly more than the "500" I would have expected.

How to parse HTML full page in android

I am calling a HTML page via a web servise . I need to get hole source code of HTML page.
My problem is that, when I convert the http response to string I am getting only some part of HTML page. How do I can get hole HTML page .Please help me.
//paramString1 = url,paramString = header, paramList = paramiters
public String a(String paramString1, String paramString2, List paramList)
{
String str1 = null;
HttpPost localHttpPost = new HttpPost(paramString1);
localHttpPost.addHeader("Accept-Encoding", "gzip");
InputStream localInputStream = null;
try
{
localHttpPost.setEntity(new UrlEncodedFormEntity(paramList));
localHttpPost.setHeader("Referer", paramString2);
HttpResponse localHttpResponse = this.c.execute(localHttpPost);
int i = localHttpResponse.getStatusLine().getStatusCode();
localInputStream = localHttpResponse.getEntity().getContent();
Header localHeader = localHttpResponse.getFirstHeader("Content-Encoding");
if ((localHeader != null) && (localHeader.getValue().equalsIgnoreCase("gzip")))
{
GZIPInputStream localObject = null;
localObject = new GZIPInputStream(localInputStream);
Log.d("API", "GZIP Response decoded!");
BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader((InputStream)localObject, "UTF-8"));
StringBuilder localStringBuilder = new StringBuilder();
while(true){
String str2 = localBufferedReader.readLine();
if (str2 == null)
break;
localHttpResponse.getEntity().consumeContent();
str1 = localStringBuilder.toString();
localStringBuilder.append(str2);
continue;
}
}
}
catch (IOException localIOException)
{
localHttpPost.abort();
}
catch (Exception localException)
{
localHttpPost.abort();
}
Object localObject = localInputStream;
return (String)str1;
Are you receiving the HTML in the variable paramString1?, in that case, are you encoding the String somehow or its just plane HTML?
Maybe the HTML special characters are breaking your response. Try encoding the String with urlSafe Base64 in your server side, and decoding it in the client side:
You can use the function Base64 of Apache Commons.
Server Side:
Base64 encoder = new Base64(true);
encoder.encode(yourBytes);
Client side:
Base64 decoder = new Base64(true);
byte[] decodedBytes = decoder.decode(paramString1);
HttpPost localHttpPost = new HttpPost(new String(decodedBytes));
You may not get the complete source code in your stringBuilder as it must be exceeding the max size of stringBuilder as StringBuilder is set of arrays. If u want to store that particular sourcecode. You may try this: The inputStream (which contains html source code) data, store directly into a File. Then you will have complete source code in that file and then perform file operation to whatever you require. See if this may help you.

Show table content from PHP to android table

i have this high score board in http://drymvizion.atwebpages.com/ and i want a simple way to click a button and show this content in my android application without opening browser. I am a bit new in android developing and i hope it's easy. Thanks ;)
You could use a HttpGet request to get the contents, or better use a xml parser, such as JSoup. For example:
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("site.name");
try
{
get.addHeader("Accept-Charset","utf-8");
HttpResponse response = client.execute(get);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String result = "";
while ((result = rd.readLine()) != null)
{
Log.e("HttpResponse", result);
if (result.length() > 0)
{
// do something with the source
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
Have a look at WebView's
http://developer.android.com/reference/android/webkit/WebView.html

how to load a big webpage into a string

I'm a novice with Java and Android, but not to programming and HTTP. This HTTP GET method, mostly copied from other examples using the Apache HTTP classes, only retrieves the first few K of a large webpage. I checked that the webpage does not have lines longer than 8192 bytes (is that possible?), but out of webpages around 40K I get back maybe 6K, maybe 20K. The number of bytes read does not seem to have a simple realtionship with the total webpage size, or the webpage modulus 8192, or with the webpage content.
Any ideas folks?
Thanks!
public static String myHttpGet(String url) throws Exception {
BufferedReader in = null;
try {
HttpClient client = getHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sbuffer = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sbuffer.append(line + "\n");
}
in.close();
String result = sbuffer.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
No need to write you own HttpEntity-to-String code, try EntityUtils instead:
// this uses the charset the server encoded the entity in
String result = EntityUtils.toString(entity);
It looks as if the problem is with pages from a certain website starting Goo... I'm not having this problem with large pages from other sites. So the code is probably OK.

Categories

Resources