I have Restful Web Service implemented by Jersey.
I connect from Android via HTTP client to fetch the data. It works fine in API level 10 and older versions but not on API level 11 or greater. I appreciate for any help. I have a nullPointerException in these versions.
public String getBaseURI(String str) {
String result = "";
try {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpGet getRequest = new HttpGet(ServerAddress + str);
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
result = getResult(response).toString();
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return result;
}
private StringBuilder getResult(HttpResponse response) throws IllegalStateException, IOException {
if (response.getStatusLine().getStatusCode() != 201)
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
StringBuilder result = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())), 1024);
String output;
while ((output = br.readLine()) != null)
result.append(output);
return result;
}
Tag : AndroidRuntime
The logcat exception:
Exception: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android.internal.os.LoggingPrintStream.Prrintln(LogiingPrintStream.java.298)
at Client.getBaseURI (Client.java:66)
when I call a rest service such as:
String str = client.getBaseURI(“task/project/get/” + user);
I get the Error.
private void listViewSet() {
ListView lv = (ListView) this.findViewById(R.id.listView);
lv.setAdapter(new MultiAdapter(this));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
switch (pos) {
case ADD:
String str = client.getBaseURI("task/project/get/" + user);// Json format
......
}
}
});
}
Are you performing the HTTP request on a separate thread (using an
AsyncTask, for example)?Some of the newer APIs (especially ICS, in my experience) forcefully require you to perform network connections on a separate thread so as to not block the UI thread. Network connections can be potentially long and expensive, so you never want to perform them on the UI thread. Doing so might prevent the UI from being drawn to the screen, cause your application to force close, make your application appear laggy to the user, etc.