Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Android
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Tim Cooke
Paul Clapham
Liutauras Vilda
Sheriffs:
Junilu Lacar
Rob Spoor
Jeanne Boyarsky
Saloon Keepers:
Stephan van Hulst
Carey Brown
Tim Holloway
Piet Souris
Bartenders:
Forum:
Android
Asynctask to fix error
Gaios Augustus
Ranch Hand
Posts: 80
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am trying to get JSON from RPC and I am keep getting error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cryptowallet/com.example.cryptowallet.UserProfile}: android.os.NetworkOnMainThreadException
I found out that application is crashing at line:
HttpResponse response = httpclient.execute(httppost);
Full code looks like:
private JSONObject invokeRPC(String id, String method, List<String> params) { DefaultHttpClient httpclient = new DefaultHttpClient(); JSONObject json = new JSONObject(); json.put("id", id); json.put("method", method); if (null != params) { JSONArray array = new JSONArray(); array.addAll(params); json.put("params", params); } JSONObject responseJsonObj = null; try { httpclient.getCredentialsProvider().setCredentials(new AuthScope("http://blockchain.oss.unist.hr", 8332), new UsernamePasswordCredentials("student", "WYVyF5DTERJASAiIiYGg4UkRH")); StringEntity myEntity = new StringEntity(json.toJSONString()); System.out.println(json.toString()); HttpPost httppost = new HttpPost("http://blockchain.oss.unist.hr:8332"); httppost.setEntity(myEntity); System.out.println("executing request" + httppost.getRequestLine()); //Problem ispod //_____________________________________________________ HttpResponse response = httpclient.execute(httppost); //_____________________________________________________ HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); // System.out.println(EntityUtils.toString(entity)); } JSONParser parser = new JSONParser(); responseJsonObj = (JSONObject) parser.parse(EntityUtils.toString(entity)); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (org.json.simple.parser.ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { httpclient.getConnectionManager().shutdown(); } return responseJsonObj; }
I tried to put it in Asynctask but I didn't know how to do it, all attempts failed.
Dave Tolls
Rancher
Posts: 4801
50
posted 3 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
What does your
AsyncTask
look like?
How is it called?
Gaios Augustus
Ranch Hand
Posts: 80
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Dave Tolls wrote:
What does your
AsyncTask
look like?
How is it called?
Well I removed it completely as I didn't know how to get it work, it always returnet me NULL object.
Stephan van Hulst
Saloon Keeper
Posts: 14781
333
posted 3 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Rewrite the asynchronous code and show it to us. We will comment on possible mistakes.
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Null Pointer Exception In Method
Trying to POST an XML to php based Web Service via HTTP POST method
Downloading JSON information and displaying it in a listview
jjoe64 GraphView disappeared suddenly in AsyncTask
More...