I’m new to android. My applcation will query to a url and will receive the json response.
But I am unable to fetch the data from the url. I am behind the proxy server ..so have updated the APN settings in the emulator and the built-in browser working fine.
I am using the following code for getting the data…:
package com.android.urlfetch;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
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 android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class UrlfetchActivity extends Activity {
Button but1;
String result ;
private TextView text;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
result = getStringContent("http://www.google.com");
}catch (Exception e) {
// TODO: handle exception
result = "Error";
}
text = (TextView)findViewById(R.id.text);
text.setText(result);
}
url=http://voxpopis.com");
public static String getStringContent(String uri) throws Exception {
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(uri));
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader (ips,"UTF-8"));
StringBuilder sb = new StringBuilder();
String s;
while(true )
{
s = buf.readLine();
if(s==null || s.length()==0)
break;
sb.append(s);
}
buf.close();
ips.close();
return sb.toString();
}
finally {
}
}
}
I tried to get the anser in stack overflow’s similar questions but dint get any answer. Any help will be appreciated.
My logcat response is :
06-30 15:14:08.820: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 601 uid 10037
06-30 15:14:10.800: WARN/PackageManager(58): Code path for pkg : com.android.urlfetch changing from /data/app/com.android.urlfetch-1.apk to /data/app/com.android.urlfetch-2.apk
06-30 15:14:10.800: WARN/PackageManager(58): Resource path for pkg : com.android.urlfetch changing from /data/app/com.android.urlfetch-1.apk to /data/app/com.android.urlfetch-2.apk
06-30 15:14:12.250: WARN/RecognitionManagerService(58): no available voice recognition services found
06-30 15:14:25.629: WARN/ActivityManager(58): Launch timeout has expired, giving up wake lock!
06-30 15:14:26.257: WARN/ActivityManager(58): Activity idle timeout for HistoryRecord{44fd4af8 com.android.urlfetch/.UrlfetchActivity}
06-30 15:14:36.911: WARN/IInputConnectionWrapper(539): showStatusIcon on inactive InputConnection
This code is working fine in direct connection ….but I am in a proxy network …its not working in proxy network.What settings I should do to work in a proxy net?
For fetching the data from the web very comfortably you can use Jsoup.To see how to work with this in android with the emulator click here