Note that there are some explanatory texts on larger screens.

plurals
  1. POHTTPGet works in Java but not Android
    primarykey
    data
    text
    <p>I am working in Android and I am having trouble with a chunk of code: </p> <pre><code>public static void main(String[] args) { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet("http://9.42.68.71:8080/resources/dashboard?dashboard=rack"); String text = null; try { HttpResponse response = httpClient.execute(httpGet, localContext); HttpEntity entity = response.getEntity(); text = getASCIIContentFromEntity(entity); } catch (Exception e) { } System.out.println(text); } </code></pre> <p>This chunk of code runs correctly on a different project in Java. The problem is that when I try to use this chunk of code in Android it errors out. </p> <pre><code>FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:3633) at android.view.View.performClick(View.java:4240) at android.view.View$PerformClick.run(View.java:17721) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at android.view.View$1.onClick(View.java:3628) ... 11 more Caused by: java.lang.NullPointerException at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298) at com.example.mgen.MgenActivity.renew(MgenActivity.java:61) at com.example.mgen.MgenActivity.refresh(MgenActivity.java:44) ... 14 more </code></pre> <p>I have the same imports on both projects. Has anyone seen an issue like this or an exception like this? I can't seem to find anything similar anywhere. </p> <p>Also I have a VPN into the local 9. network and I can access it both from the android vm internet browser and my local laptop. </p> <p>I have also troubleshot to find the issue on the Android version is on the line </p> <pre><code>HttpResponse response = httpClient.execute(httpGet, localContext); </code></pre> <p>More complete code: </p> <pre><code>package com.example.mgen; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.HttpContext; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class MgenActivity extends Activity { ArrayList&lt;mgenObject&gt; listItems = new ArrayList&lt;mgenObject&gt;(); ArrayAdapter&lt;mgenObject&gt; adapter; public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); adapter = new ArrayAdapter&lt;mgenObject&gt;(this, android.R.layout.simple_list_item_1, listItems); ListView lv = (ListView) findViewById(R.id.list); lv.setAdapter(adapter); renew(); } // METHOD WHICH WILL HANDLE DYNAMIC INSERTION public void refresh(View v) { listItems = renew(); adapter.notifyDataSetChanged(); System.out.println("refresh worked"); } public ArrayList&lt;mgenObject&gt; renewTest() { mgenObject object = new mgenObject("test", "test", "test"); ArrayList&lt;mgenObject&gt; list = new ArrayList&lt;mgenObject&gt;(); list.add(object); return list; } public ArrayList&lt;mgenObject&gt; renew() { ArrayList&lt;mgenObject&gt; tempList = new ArrayList&lt;mgenObject&gt;(); String jsonStr = getMgenJSON(); System.out.println(jsonStr); try { JSONArray rows = new JSONArray(jsonStr); // Parse the JSON to a // JSONArray for (int i = 0; i &lt; rows.length(); i++) { // Loop over each each row JSONObject element = rows.getJSONObject(i); // Get row object String workflow = element.getString("workflow_state"); String vgen = element.getString("vgen_state"); String name = element.getString("title"); mgenObject tempObject = new mgenObject(name, workflow, vgen); tempList.add(tempObject); } } catch (JSONException e) { // JSON Parsing error e.printStackTrace(); } return tempList; } public String getMgenJSON() { HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpGet httpGet = new HttpGet( "http://9.42.68.71:8080/resources/dashboard?dashboard=rack"); String text = null; try { System.out.println("testing3"); HttpResponse response = httpClient.execute(new HttpGet("http://9.42.68.71:8080/resources/dashboard?dashboard=rack")); System.out.println("testing3.5"); HttpEntity entity = response.getEntity(); System.out.println("testing4"); text = getASCIIContentFromEntity(entity); System.out.println("testing5"); } catch (Exception e) { return e.getLocalizedMessage(); } System.out.println(text); return text; } protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { InputStream in = entity.getContent(); StringBuffer out = new StringBuffer(); int n = 1; while (n &gt; 0) { byte[] b = new byte[4096]; n = in.read(b); if (n &gt; 0) out.append(new String(b, 0, n)); } return out.toString(); } } </code></pre> <p>Also if you cannot tell, I am trying to make this call when a button is clicked.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload