android从url中获取数据
我是安卓新手。我的应用程序将查询 url 并收到 json 响应。 但我无法从 url 获取数据。我在代理服务器后面..所以已经更新了模拟器中的 APN 设置,并且内置浏览器工作正常。
我正在使用以下代码来获取数据...:
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 {
}
}
}
我尝试在堆栈溢出的类似问题中获取答案,但没有得到任何答案。任何帮助将不胜感激。
我的 logcat 响应是:
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
此代码在直接连接中工作正常...但我在代理网络中...它无法在代理网络中工作。我应该做哪些设置才能在代理网络中工作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要非常轻松地从网络获取数据,您可以使用 Jsoup。要了解如何使用模拟器在 android 中使用此数据,请单击 这里
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