emulator - internet connection issue
I have set my proxy settings in emulator and I can access normal internet on emulator's browser. But when I try to run my application, it gives error But when I change my URL to services running inside my organisation I can fetch data!!!
What is the issue here and how could I connect to URLs outside my org through my app???
Thanks Abhinav Tyagi
ERROR:
05-11 17:12:28.867: WARN/System.err(847): java.io.FileNotFoundException: http://google.co.in 05-11 17:12:28.917: WARN/System.err(847):
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162) 05-11 17:12:28.917: WARN/System.err(847): at com.xml.xml_test.getXML(xml_test.java:65) 05-11 17:12:28.928: WARN/System.err(847): at java.lang.reflect.Method.invokeNative(Native Method) 05-11 17:12:28.939: WARN/System.err(847): at java.lang.reflect.Method.invoke(Method.java:521) 05-11 17:12:28.957: WARN/System.err(847): at android.view.View$1.onClick(View.java:2067) 05-11 17:12:28.957: WARN/System.err(847): at android.view.View.performClick(View.java:2408) 05-11 17:12:28.968: WARN/System.err(847): at android.view.View$PerformClick.run(View.java:8816) 05-11 17:12:28.968: WARN/System.err(847): at android.os.Handler.handleCallback(Handler.java:587) 05-11 17:12:28.989: WARN/System.err(847): at android.os.Handler.dispatchMessage(Handler.java:92) 05-11 17:12:28.989: WARN/System.err(847): at android.os.Looper.loop(Looper.java:123) 05-11 17:12:29.007: WARN/System.err(847): at android.app.ActivityThread.main(ActivityThread.java:4627) 05-11 17:12:29.007: WARN/System.err(847): at java.lang.reflect.Method.invokeNative(Native Method) 05-11 17:12:29.007: WARN/System.err(847): at java.lang.reflect.Method.invoke(Method.java:521) 05-11 17:12:29.007: WARN/System.err(847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-11 17:12:29.007: WARN/System.err(847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-11 17:12:29.037: WARN/System.err(847): at dalvik.system.NativeStart.main(Native Method)
here is my code:
StringBuffer b = new StringBuffer();
System.out.println("try");
String registrationUrl = "http://google.com";
url = new URL(registrationUrl);
System.out.println("url");
URLConnection connection = url.openConnection();
System.out.println("open connection");
conn = (HttpURLConnection) connection;
int responseCode = conn.getResponseCode();
System.out.println(responseCode);
is = conn.getInputStream();
long len = 0;
int ch = 0;
len = conn.getContentLength();
if (len != -1) {
// Read exactly Content-Length bytes
System.out.println("IF...");
for (int i = 0; i < len; i++) {
System.out.println("For Loop...");
if ((ch = is.read()) != -1) {
System.out.println("Appending");
b.append((char) ch);
}
开发者_如何学Python }
} else {
// Read till the connection is closed.
System.out.println("Else...");
while ((ch = is.read()) != -1) {
System.out.println("When...");
len = is.available();
System.out.println("append");
b.append((char) ch);
}
}
String data = b.toString();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(data).setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
xml_test.this.getParent();
}
});
I dont have much time to go through what you actually need but if you wanna apply proxy to your application check the following examples..
http://www.jguru.com/faq/view.jsp?EID=13186
http://www.rgagnon.com/javadetails/java-0085.html
http://www.java2s.com/Code/Java/Network-Protocol/URLconnectionandproxy.htm
they're pretty much the same. sorry.. and good luck :)
精彩评论