我是否必须设置代理以在Android中使用Web服务?

时间:2012-08-07 14:54:47

标签: android json web-services proxy

我正在开发一台与LAN中的服务器连接的计算机.proxy用于在我的计算机上访问互联网,现在我正在进行消费php web服务的项目,该服务将JSON作为输出。我已经为它制作了一个应用程序但它给出了空白响应,我已经阅读了一个堆栈溢出问题答案,说它是因为代理。 我应该在我的应用程序中设置代理设置。 我怎样才能为这个问题设置代理。

我已按此步骤设置代理

  1. 点击菜单
  2. 点击“设置”
  3. 点击Wireless&网络
  4. 转到移动网络
  5. 转到接入点名称
  6. 在这里,您将Telkila Internet,点击它。
  7. 在“编辑接入点”部分中,输入“代理”和“端口”
  8. 还提供用户名和密码,其余字段留空
  9. 互联网正在模拟器中工作,但仍然没有显示项目。 这是我的代码

    package com.example.jsonexaple2;
    
    import java.io.BufferedInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import org.apache.http.util.ByteArrayBuffer;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    
    public class Jsonexaple2 extends Activity {
        String archiveQuery = "http://www.archive.org/advancedsearch.php?q=Lotus&fl[]=date&fl[]=format&fl[]=identifier&fl[]=mediatype&fl[]=title&sort[]=createdate+desc&sort[]=&sort[]=&rows=10&page=1&output=json&callback=callback&save=yes";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.jsonexaple2);
            InputStream in = null;
            String queryResult = "";
            try {
             URL url = new URL(archiveQuery);
             HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
             HttpURLConnection httpConn = (HttpURLConnection) urlConn;
             httpConn.setAllowUserInteraction(false);
             httpConn.connect();
             in = httpConn.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(in);
             ByteArrayBuffer baf = new ByteArrayBuffer(50);
             int read = 0;
             int bufSize = 512;
             byte[] buffer = new byte[bufSize];
             while(true){
              read = bis.read(buffer);
              if(read==-1){
               break;
              }
              baf.append(buffer, 0, read);
             }
             queryResult = new String(baf.toByteArray());
            } catch (MalformedURLException e) {
             // DEBUG
             Log.e("DEBUG: ", e.toString());
            } catch (IOException e) {
             // DEBUG
             Log.e("DEBUG: ", e.toString());
            }
            JSONObject jObject;
            try {
             jObject = new JSONObject(queryResult.replace("callback(", "")).getJSONObject("response");
             JSONArray docsArray = jObject.getJSONArray("docs");
             for (int i = 0; i < 10; i++) {
              if (docsArray.getJSONObject(i).optString("mediatype").equals("etree")) {
               String title = docsArray.getJSONObject(i).optString("title");
               String identifier = docsArray.getJSONObject(i).optString("identifier");
               String date = docsArray.getJSONObject(i).optString("date");
               System.out.println(title + " " + identifier + " " + date);
              }
             }
            } catch (JSONException e) {
             // DEBUG
             Log.e("DEBUG: ", e.toString());
            }
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

是的,你必须这样做。有一次,我和模拟器有同样的问题。我在Google上搜索过,我发现这两个博客很不错。

希望它会对你有所帮助。