Force Close asynctask (Illegal Character) - android

I am getting this error, which is odd because it works from another activity calling the same async task. I can not figure out what the illegal character is in my query:
09-06 17:42:29.098 32101-32497/com.beerportfolio.beerportfoliopro E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 50: http://beerportfolio.com/app_getTopTaste.php?t=Rum
at java.net.URI.create(URI.java:727)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
at com.example.beerportfoliopro.GetTopTasteBeersJSON.readJSONFeed(GetTopTasteBeersJSON.java:139)
at com.example.beerportfoliopro.GetTopTasteBeersJSON.doInBackground(GetTopTasteBeersJSON.java:49)
at com.example.beerportfoliopro.GetTopTasteBeersJSON.doInBackground(GetTopTasteBeersJSON.java:34)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more

Illegal character in query at index 50: http://beerportfolio.com/app_getTopTaste.php?t=Rum
It's the character at index 50, which is right after "Rum". It's probably some sort of whitespace character. You'll have to post your code for how you get/generate that URL if you want more details, but you might need to add some code to strip whitespace somewhere.

try to trim the string. It removes unwanted whitespace at the end of the string.
String url = " http://beerportfolio.com/app_getTopTaste.php?t=Rum";
HttpGet request = new HttpGet(url.trim());

Try the below
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
String url = " http://beerportfolio.com/app_getTopTaste.php?t=Rum";
HttpGet request = new HttpGet(url);
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
String _response=EntityUtils.toString(resEntity);
Log.i(".......",_response);
I just tried your url and i do get the response
09-06 22:05:04.547: I/.......(1460): [{"beer":"Rum Cask","rate":"5","id":"dkRDyR","breweryID":"jC0TAa"}]
As kabuko stated character 50 is after Rum. I think kabuko is right.
I guess you have space at the end of url. It is better to encode the url as below.
String query = URLEncoder.encode("Rum ", "utf-8");
String url = "http://beerportfolio.com/app_getTopTaste.php?t=" + query;
HttpGet request = new HttpGet(url);
URL encoding in Android

Related

Spring Basic OAuth throwing bad Request Exception

Hi i am trying to connect with elastic search using Spring resttemplate but it gives exception saying bad request. below is my code
String plainCreds = "elasticsearch:*****";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic " + base64Creds);
final String url = "https://ff92ba95318093026b7a06180f2b2d19.us-east-1.aws.found.io:9243/jinkjobposts_dev_v1/jobPosts";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpEntity<String> request = new HttpEntity<>( headers);
Log.d("location", "before exchange");
ResponseEntity<JobPosts> response = restTemplate.exchange(url, HttpMethod.POST, request, JobPosts.class);
JobPosts jobPosts = response.getBody();
Log.d("location", "after exchange");
list = Arrays.asList(jobPosts);
it gives this exception.
FATAL EXCEPTION: AsyncTask #2
Process: in.thoughtsmith.jink, PID: 8902
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1115)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:590)
at java.lang.Thread.run(Thread.java:818)
Caused by: org.springframework.web.client.HttpClientErrorException: 400 Bad Request
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:76)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:524)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:481)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
at in.thoughtsmith.jink.MapsActivity$SearchJob.doInBackground(MapsActivity.java:690)
at in.thoughtsmith.jink.MapsActivity$SearchJob.doInBackground(MapsActivity.java:645)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1115) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:590) 
at java.lang.Thread.run(Thread.java:818) 
Please me i am doing this for the first time
It's not an authorization issue. Your url is simply missing the _search endpoint at the end, hence the error HTTP 400 you're getting.
final String url = "https://ff92ba95318093026b7a06180f2b2d19.us-east-1.aws.found.io:9243/jinkjobposts_dev_v1/jobPosts/_search";

MultipartEntityBuilder on API 22

I want to upload an image to server but i need to send 2 more things in order to connect with the server any HELP cause im really stuck on this one.
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// MultipartEntity entity = new MultipartEntity();
try {
File file = new File(path);
// Adding file data to http body
builder.addPart("image", new FileBody(file));
// Extra parameters if you want to pass to server
builder.addTextBody("confid", name);
builder.addTextBody("udid", id);
entity = builder.build();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
Here are the errors from android Studio logcat
0-08 12:06:01.216 8616-8787/? E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntityBuilder
at com.example.oleg.myapplication.MainActivity$1.uploadFile(MainActivity.java:305)
at com.example.oleg.myapplication.MainActivity$1.doInBackground(MainActivity.java:285) at com.example.oleg.myapplication.MainActivity$1.doInBackground(MainActivity.java:279) at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)

NullPointerException only when on Google Play Store?

I recently uploaded an update of my app to Google Play. When trying it out, I get an error when clicking on a manually added banner (e.g. not AdMob or anything like that). The click is setting of an AsyncTask that sends data to a php-script using POST. I use ProGuard so when I send the error report to myself I am only able to see the method in which the error is caused: doInBackground. I am also able to see that it is a NullPointerException. The strange thing is, when I run the app on my device from my computer using Eclipse, the error doesn't occur, so I can't really find what line is causing the NullPointerException. It has worked perfectly in previous versions of my app, and I haven't made any changes to this code. And when I look through the method doInBackground, I can't find any place where a NullPointerException might occur.
Here is the error message from my developers console, this is proguarded though:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at se.nollekollen.main.d.a(Unknown Source)
at se.nollekollen.main.d.doInBackground(Unknown Source)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
And here is my doInBackground:
#Override
protected String doInBackground(String... arg0) {
String url_stats = "http://xxxxx.xx/statistik/sendStatistics_click.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("activity", getClass().getEnclosingClass().getName().substring(20)));
params.add(new BasicNameValuePair("section", SECTION));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_stats);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
The only thing that could be null here is actually "SECTION", but that wouldn't cause a NullPointerException since the value can be null when creating a new BasicNameValuePair(key, value).
Can anyone find what could cause the NullPointerException? And how is it possible that this ONLY occurs when the app is downloaded from Google Play, and not when run from the computer?

StringIndexOutOfBoundsException in android

I am getting the following error:
java.lang.StringIndexOutOfBoundsException: length=13243; regionStart=32; regionLength=-39
at java.lang.String.startEndAndLength(String.java:593)
at java.lang.String.substring(String.java:1474)
at com.dict.XMLParser.getResultFromXML(XMLParser.java:63)
at com.dict.InternetDictProvider.searchWord(InternetDictProvider.java:29)
at com.dict.SearchDict$SearchOnline.doInBackground(SearchDict.java:130)
at com.dict.SearchDict$SearchOnline.doInBackground(SearchDict.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
in the following code which typically gets results after parsing the XML page given as HttpResponse to a HttpGet :
retry:
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://oxforddictionaries.com/definition/"+query+"?q="+query);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String meanings=parser.getResultFromXML(StringUtils.inputStreamToString(httpEntity.getContent()));
if(meanings==null && firstRetry)
{
firstRetry=false;
query = query.substring(0, 1).toUpperCase() + query.substring(1);
break retry;
}
else if(meanings==null && !firstRetry)
return query;
result = query + ":" + meanings;
}
query = query.substring(0, 1).toUpperCase() + query.substring(1);
i think this statement create exeption so use if(null!=query&&query.length()!=0){query=...}
and if you substring your string length more than 1 in your case

Sending JSON from Android to .NET WCF with international characters. How?

I'm getting exceptions when I'm trying to POST and my JSON has some international characters.
This is code that I use:
HttpPost request = new HttpPost(serviceURL + url);
request.addHeader("Authorization", "Basic " + Preferences.getAuthorizationTicket(mContext));
request.addHeader("DeviceSerialNumber", Utility.getDeviceSerialNumber(mContext));
request.addHeader("OSVersion", "Android v" + Build.VERSION.RELEASE);
StringEntity entity = new StringEntity(requestData);
entity.setContentType("application/json;charset=UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
request.setEntity(entity);
ResponseHandler<String> handler = new BasicResponseHandler();
response.Body = mHttpClient.execute(request, handler);
response.Code = HttpURLConnection.HTTP_OK;
response.Message = "OK";
And this is error I'm getting:
org.apache.http.client.HttpResponseException:
Bad Request at
org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
at
org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
at
com.idatt.common.AsyncProcessor.processPOST(AsyncProcessor.java:550)
at
com.idatt.common.AsyncProcessor.PostMail(AsyncProcessor.java:367)
at
com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:120)
at
com.idatt.common.AsyncProcessor.doInBackground(AsyncProcessor.java:28)
at
android.os.AsyncTask$2.call(AsyncTask.java:185)
at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at
java.util.concurrent.FutureTask.run(FutureTask.java:137)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at
java.lang.Thread.run(Thread.java:1096)
When requestData doesn't have any international (russian, polish, etc) letters than it works fine. What do I miss? Or better yet how do I get traffic from emulator captured in Fiddler or something?
I believe you should specify UTF-8 charset when you create StringEntity:
StringEntity entity = new StringEntity(requestData,"utf-8");
http://developer.android.com/reference/org/apache/http/entity/StringEntity.html#StringEntity(java.lang.String, java.lang.String)

Categories

Resources