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)
Related
How do you handle the RuntimeException when AsyncTask couldn't connect to server since server is "down" for some reason?
What do I try is a bunch of catch blocks (doesn't help):
try {
// Create Request to server and get response
HttpGet httpget = new HttpGet(activity.getString(R.string.get_channels_lang_host));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
downloadedString = httpclient.execute(httpget, responseHandler);
}catch (MalformedURLException e) {
Log.e("URL:","is a malformed URL");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.e("URL:"," UnsupportedEncodingException");
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("URL:"," ClientProtocolException");
} catch (SocketTimeoutException e) {
e.printStackTrace();
Log.e("URL:"," SocketTimeoutException");
} catch (ConnectTimeoutException e) {
e.printStackTrace();
Log.e("URL:"," ConnectTimeoutException");
} catch (IOException e) {
Log.e("URL:","IOException");
e.printStackTrace();
}
if (downloadedString != null) {
//parse request to object
dataChannelsLangArrayList = JsonParser.getDataChannelsLang(downloadedString);
} else {
Toast.makeText(ActivityLoading.this, "Failed to load data. Restarting...", Toast.LENGTH_LONG).show();
Global.restartApp(ActivityLoading.this);
}
i'm still getting the runtime error:
04-09 11:31:29.610 3696-3719/tenkol.design.com.imbrecords E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
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.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.widget.Toast$TN.<init>(Toast.java:322)
at android.widget.Toast.<init>(Toast.java:91)
at android.widget.Toast.makeText(Toast.java:238)
at tenkol.design.com.imbrecords.Global.restartApp(Global.java:115)
at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.getChannelsLang(ActivityLoading.java:249)
at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.doInBackground(ActivityLoading.java:157)
at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.doInBackground(ActivityLoading.java:141)
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:864)
I'm mostly interested in two cases:
When server doesn't respond at all;
When the time spend for request is too big (i.e. with slow Internet).
What is the proper way to handle those?
Can't create handler inside thread that has not called Looper.prepare()
Well, you have created a Handler, but the thread it is linked to doesn't have any message queue to post stuff on.
Try new Handler(Looper.getMainLooper()) for a handler linked to main thread.
Then you can post a Runnable on it which shows a toast.
But seriously, why not collect exception and result both and handle it all in onPostExecute() ?
Public class Result<T>{
public T data;
public Exception exception;
public String message;
}
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
I am using the below code to fetch xml file from the server and due to heavy and large xml file, it got crashed and showing Out of memory Issue.
public class Connect {
static BufferedReader in=null;
String result=null;
Context context;
//Establish connection with web server
public String HTTPConnect(String uri1,List<NameValuePair> list,Context context)
{
this.context=context;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uri1);
if(list!=null)
{
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
httpPost.setEntity(formEntity);
}
HttpResponse httpResponse = httpClient.execute(httpPost);
in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
long heapSize = Runtime.getRuntime().totalMemory();
if(in!=null)
{
while ((line = in.readLine()) != null) {//crasheg here
sb.append(line + NL);
}
in.close();
}
result = sb.toString();
}
catch(UnsupportedEncodingException e)
{
String err = (e.getMessage()==null)?"Cant connect to server":e.getMessage();
ShowDialog();
}
catch (MalformedURLException e) {
String err = (e.getMessage()==null)?"Malformed Exception":e.getMessage();
ShowDialog();
}
catch(Exception ex)
{
String err = (ex.getMessage()==null)?"NetworkConnectionException":ex.getMessage();
ShowDialog();
}
finally {
if (in != null) {
try {
in.close();
} catch (Exception ex) {
String err = (ex.getMessage()==null)?"Excepion":ex.getMessage();
ex.printStackTrace();
}
}
}
return result;
}
I know that that I am copying the whole XML in String and due to heavy file, It gor crashing. It is working fine in small size of XML file, but what are the alternative for large xml file. I am uing SAX parser for parsing this xml file.
[EDIT]
Below is the logcat:
FATAL EXCEPTION: AsyncTask #4
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
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 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)
Caused by: java.lang.OutOfMemoryError
java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:97)
java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:136)
java.lang.StringBuilder.append(StringBuilder.java:272)
java.io.BufferedReader.readLine(BufferedReader.java:452)
com.kxs.appitize.Connect.HTTPConnect(Connect.java:56)
com.kxs.appitize.ListRestaurants$Asyn_rest.doInBackground(ListRestaurants.java:168)
com.kxs.appitize.ListRestaurants$Asyn_rest.doInBackground(ListRestaurants.java:1)
01-at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
Activity com.kxs.appitize.TabsMainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#44f4fb28 that was originally added here
android.view.WindowLeaked: Activity com.kxs.appitize.TabsMainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#44f4fb28 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:247)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at com.kxs.appitize.ListRestaurants$Asyn_rest.onPreExecute(ListRestaurants.java:156)
at android.os.AsyncTask.execute(AsyncTask.java:391)
at com.kxs.appitize.ListRestaurants.onCreate(ListRestaurants.java:133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at com.kxs.appitize.TabGroupActivity.startChildActivity(TabGroupActivity.java:72)
at com.kxs.appitize.ListCategories$1.onClick(ListCategories.java:109)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
copying the whole XML in String and due to heavy file
Bad idea. First you need to cache the xml in a temporary directory. It should never be in the RAM.
Once you do that use SAX to parse the file. During parsing try not to keep the whole structure in the RAM, instead parse in bite sized chunks.
Try to use inputStream to parse XML. The inputStrem does not increase Android stack size and it's prevent possibles OOM with large String. I have the same things with large JSON and now, i use Jackson to parse JSON directly from Stream.
You can find equivalent library for parsing XML.
I am reading the youtube response in Stream and converting it to String. using the below code:
URL: http://gdata.youtube.com/feeds/api/users/cnn/uploads?&v=2&max-results=25&alt=json
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
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();
}
This code is working in fine in all devices except Android 4.0 devices. I am getting the below logcat:
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.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
at java.lang.StringBuilder.append(StringBuilder.java:216)
at com.android.mypack.MyApplications.convertStreamToString(MyApplications.java:959)
at com.android.mypack.MyApplications.access$61(MyApplications.java:951)
at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:2303)
at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 5 more
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
at java.lang.StringBuilder.append(StringBuilder.java:216)
at com.android.mypack.MyApplications.convertStreamToString(MyApplications.java:959)
at com.android.mypack.MyApplications.access$61(MyApplications.java:951)
at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.java:2303)
at com.android.mypack.MyApplications$YoutubeTask.doInBackground(MyApplications.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)
Please help me to overcome this error.
Thanks in Advance.
Well, OutOfMemoryErrors are coming obviously when the virtual machine runs out of memory. The important point here is that the exact stack trace might not be informative, because it could happen that another thread uses all of the memory. Or something memory-consuming happened on this thread before this code, and this code was only the "last straw". This URL certainly does not contain too many bytes, I think the problem is in another place.
You could try a memory analyzer:
Android ==> Memory Analysing ==> Eclipse memory analyzer?
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