I am making a network call as instructed in developer.android.com (by using HttpURLConnection). Now I am getting a out of memory exception and app is getting crashed.
The following is the stack trace:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.app, PID: 19869
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 4294967306 byte allocation with 4056144 free bytes and 370MB until OOM
at com.example.app.webservices.HttpConnectionClass.readIt(HttpConnectionClass.java:158)
at com.example.app.webservices.HttpConnectionClass.downloadUrl(HttpConnectionClass.java:123)
at com.example.app.webservices.HttpConnectionClass.access$100(HttpConnectionClass.java:28)
at com.example.app.webservices.HttpConnectionClass$DownloadWebpageTask.doInBackground(HttpConnectionClass.java:52)
at com.example.app.webservices.HttpConnectionClass$DownloadWebpageTask.doInBackground(HttpConnectionClass.java:46)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
I've already included this in my manifest :
android:largeHeap="true"
This is my Networkclass:
public class HttpConnectionClass {
public static Activity context;
List json;
static boolean dialogIsShowing = true;
public String url;
List<NameValuePair> params = new ArrayList<NameValuePair>();
public void getPostResponse_WithAsyn(Activity context, String url, List json, String message) {
this.params = json;
this.url= url;
this.json = json;
Log.v("TAG","PARAMS::"+json.toString());
//add all the default parameters here
new DownloadWebpageTask().execute(url);
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
e.printStackTrace();
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
#Override
protected void onPostExecute(String result) {
GetHttpPostResponse.myJsonResponse = result;
/*int maxLogSize = 1000;
for(int i = 0; i <= result.length() / maxLogSize; i++) {
int start = i * maxLogSize;
int end = (i+1) * maxLogSize;
end = end > result.length() ? result.length() : end;
Log.v("TAG_result", result.substring(start, end));
}
*/
Log.v("Connection class","Response::"+result);
}
}
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len =Integer.MAX_VALUE;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Log.v("TAG","CONNECTING URL::"+myurl);
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("key1", ""));
params.add(new BasicNameValuePair("key2", ""));
Log.v("TAG","PARAMETERS::"+params.toString());
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
// Starts the query
conn.connect();
int response = conn.getResponseCode();
String res_data = conn.getResponseMessage();
Log.d("TAG", "The response is: " + res_data);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
private String getQuery(List<NameValuePair> params) throws IOException,UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
}
And,I've Identified that "len" is the problem and tried to hardcode it but doesn't seem to work in all scenarios.
{"ResponseCode":"-1000","ResponseDesc":"Invalid user type."}�
Please help me out.
Try this readIt method,
public String readIt(InputStream stream) throws IOException, UnsupportedEncodingException {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String line;
while ((line = br.readLine()) != null){
sb.append(line);
}
br.close();
return sb.toString();
}
You are trying to do
int len =Integer.MAX_VALUE;
char[] buffer = new char[len];
do you have that much memory?
You are allocating char array of length Integer.MAX_LENGTH, which will not fit in the available memory. I think you would be better of to have readIt() look something like this:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int l;
while ((l = stream.read(buffer)) != -1) {
baos.write(buffer, 0, l);
}
return baos.toString("UTF-8");
Related
A script on my server returns an array of SQL rows. An appropriate curl returns string which is correct:
[{"ID":"1","DATETIME":"2021-05-30 21:29:27","SETPOINT":"45.22","LUMIN":"65.98","DIRECTION":"LOG"},{"ID":"1","DATETIME":"2021-05-30 15:55:34","REVS":"2.11934","ROTATION":"1","DIRECTION":"LOG","POSITION":"12"}]
However, while parsing it in my Android application, I am getting an error org.json.JSONException: Value http of type java.lang.String cannot be converted to JSONArray. Closer investigation (Toast printing) to what is passed indicated that the application is receiving the remote script's address http://<ip>/readLatest.php, and not the array, as seen with the curl query.
What seems to be a problem? Other answers as to similar questions were not helpful
Async task:
public class DataGetter extends AsyncTask<String, Void, String[]> {
Context context;
private GetListener GetListener;
public String jsonString;
public boolean passed_successfully;
DataGetter(Context ctx, GetListener listener) {
this.context = ctx;
this.GetListener = listener;
this.passed_successfully = false;
}
#Override
protected String[] doInBackground(String... params) {
String ip = params[0];
String scriptname = params[1];
String db = params[2];
String urladress = "http://" + ip + "/"+ scriptname +".php";
try {
URL url = new URL(urladress);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");
String data = URLEncoder.encode("database", "UTF-8") + "=" + URLEncoder.encode(db, "UTF-8");
writer.write(data);
writer.flush();
writer.close();
BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
break;
}
connection.disconnect();
return new String[] {sb.toString(), scriptname};
} catch (Exception e) {
return new String[] {e.getMessage()};
}
}
#Override
protected void onPostExecute(String... result) {
GetListener.passJSONGet(result);
}
}
Part of appropriate Activity:
#Override
public void passJSONGet(String... result) {
String jsonstring = result[0];
try {
showToast(jsonstring);
this.jsonArray = new JSONArray(jsonstring);
this.printControls(jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}
}
private void printControls(JSONArray jsonArray) throws JSONException {
String lumin = jsonArray.getJSONObject(1).getString("LUMIN");
this.luminval.setText(String.format("%s", lumin));
int position = Integer.parseInt(jsonArray.getJSONObject(0).getString("POSITION"));
this.positionval.setText(String.format("%s", position));
}
I wrote this code to make a connection and I used AsyncTask class BufferedReader with JSON object and JSON Array, but the bufferedReader return null every time, I tried the API by Fiddler (HTTP debugging server) and it is worked correctly.
//class AsyncTask
class GetUserList extends AsyncTask<String, Void, String> {
String status= null;
public GetUserList(){
}
protected void onPreExecute(){
}
#SuppressLint("WrongThread")
protected String doInBackground(String... connUrl){
HttpURLConnection conn=null;
BufferedReader reader;
final URL url;
int result;
InputStream in;
String line;
try{
url=new URL(connUrl[0]);
conn=(HttpURLConnection) url.openConnection();
conn.addRequestProperty("Content-Type", "application/json; charset=utf-8");
conn.setRequestMethod("GET");
result = conn.getResponseCode();
if(result == 200){
in=new BufferedInputStream(conn.getInputStream());
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
line = null;
while((line=reader.readLine())!=null){
status=line;
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return status;
}
protected void onPostExecute(String result){
super.onPostExecute(result);
JSONArray jsonArray;
JSONObject object;
String User;
int i =0;
if(result!=null){
try{
jsonArray = new JSONArray(result);
for( i=0; i<jsonArray.length(); i++){
object = jsonArray.getJSONObject(i);
User= object.getString("User");
}//end for
}//end try
catch (Exception ex){
ex.printStackTrace();
}//end catch
}//end if
}
}
Error which i am facing
The Error Tells me on class execution to create and inner class or create a new class
But i am executing it inside a button which is in onCreate method so please if anybody can guide me how to proceed ....
The Main Class
private class SendDeviceDetails extends AsyncTask {
#Override
protected String doInBackground(String... params) {
String data = "";
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData=" + params[1]);
wr.flush();
wr.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.e("TAG", result); // this is expecting a response code to be sent from your server upon receiving the POST data
}
}
You missed () after newSendDeviceDetails. It should be like the line below:
new newSendDeviceDetails().execute(...);
I have this method which is giving a network on main thread. I want to make this api call on a separate thread using asynctask.
However, the business logic prohibits me to use non static methods. The code is:
public static JSONObject acceptOrder(String orderId, Integer loadsToAccept) throws IOException{
BufferedReader reader = null;
JSONObject resultOrder = null;
AsyncTask asyncTask = new AsyncTask() {
#Override
protected Object doInBackground(Object[] objects) {
return null;
}
};
HttpURLConnection conn = (HttpURLConnection) new URL(GlobalConfig.getInstance().GetGoVulcanConfig().getUrl() + "/api/Order/ProcessAcceptedOrder?acceptedLoads=" + loadsToAccept + "&haulerId=" + GlobalConfig.getInstance().GetGoVulcanConfig().getHaulerId() + "&orderId=" + orderId).openConnection();
conn.setDoOutput(true);
//conn.setFixedLengthStreamingMode(jsonString.length());
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept","application/json");
InputStream inputStream = conn.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null)
buffer.append(inputLine);
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
try {
resultOrder = new JSONObject(buffer.toString());
}catch (JSONException ex){
Log.d(ex.getMessage(), "acceptOrder: ");
}
conn.disconnect();
reader.close();
return resultOrder;
}
How can I do this?
This is the form that i do to request POST or GET, I hope can help you
1) I have my class with all the request
public class RequestManager {
public static class requestPostExample extends AsyncTask<String, Void, String>{
Context context;
int exampleId;
String exampleData;
//interface to get the response in the activity
Public AsynkTaskRespone response = null;
public requestPostExample(Context context, int exampleID, String exampleData){
this.context = context;
this.exampleId = exampleID;
this.exampleData = exampleData;
}
#Override
protected String doInBackground(String... params) {
try {
String urlString = "yourUrl";
URL url = new URL(urlString);
//the variables and data you want to send by POST
String postData = "exampleID="+exampleID+"&exampleData="+exampleData;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStream os = connection.getOutputStream();
os.write(postData.getBytes());
StringBuilder responseSB = new StringBuilder();
int result = connection.getResponseCode();
BufferedReader br;
// 401 - 422 - 403 - 500
if (result == 401 || result == 422 || result == 403 || result == 404 || result == 500){
br = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
}else {
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
while ( (JSON_STRING = br.readLine()) != null)
responseSB.append(JSON_STRING+ "\n");
// Close streams
br.close();
return responseSB.toString().trim();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
#Override
protected void onPostExecute(String result) {
Log.d("Result:", result);
//send the result to interface;
response.resultPostExample(result);
}
}
2) Here is the Interface
public interface AsynkTaskRespone {
void resultPostExample(String result);
}
3) Now in the activity
public class MainActivity extends AppCompatActivity implements AsynkTaskResponse {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Start Request
RequestManager.requestPostExample requestPostExample = new RequestManager.requestPostExample(this, exampleID, exampleData);
requestPostExample.response = this;
requestPostExample.execute();
}
#Override
public void resultPostExample(String result){
//here you get the result of the asynktask
}
}
I'm trying to post HTTP from android. If I'll request using that C# code in LINQPAD it works
void Main()
{
string r = String.Format(#"<s:Body><TrackMobileApp xmlns=""soap action url"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><device>test</device><imei>test</imei><ipAddress>127.0.0.1</ipAddress><timeStamp>2016-02-17T17:32:00.5147663+04:00</timeStamp></TrackMobileApp></s:Body>", DateTime.Now.ToString("o"));
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("URL.asmx");
wr.ProtocolVersion = HttpVersion.Version11;
wr.Headers.Add("SOAPAction", "soap action");
wr.Method = "POST";
wr.ContentType = "text/xml;charset=utf-8";
using (StreamWriter sw = new StreamWriter(wr.GetRequestStream()))
{
sw.Write(r);
}
HttpWebResponse rs = (HttpWebResponse)wr.GetResponse();
if (rs.StatusCode == HttpStatusCode.OK)
{
XmlDocument xd = new XmlDocument();
using (StreamReader sr = new StreamReader(rs.GetResponseStream()))
{
xd.LoadXml(sr.ReadToEnd());
xd.InnerXml.Dump();
}
}
}
But from android it gives me http 415. What's wrong?
I have this in onCreate
new DownloadTask().execute("URL.asmx");
and my methods are:
private class DownloadTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//do your request in here so that you don't interrupt the UI thread
try {
return downloadContent(params[0]);
} catch (IOException e) {
return "Unable to retrieve data. URL may be invalid.";
}
}
#Override
protected void onPostExecute(String result) {
//Here you are done with the task
}
}
private String downloadContent(String myurl) throws IOException {
InputStream is = null;
int length = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestProperty("SOAPAction", "soap action");
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/xml");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
Log.d(TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = convertInputStreamToString(is, length);
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
public String convertInputStreamToString(InputStream stream, int length) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[length];
reader.read(buffer);
return new String(buffer);
}