-1

我在 localhost 上创建了一个 WCF 服务,它在调用时返回一个 json 我想在 android 客户端上获取 json 但它不工作,我尝试从一个示例 android 教程站点获取一个 json 并且工作但我自己的 localhost WCF 服务似乎不起作用

package com.example.usa;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class Home extends Activity {
    // url to make request
    private static String url = "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary/Service1/GetDataUsingDataContract";
                               //http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary/Service1/
    // JSON Node names
    private static final String TAG_CONTACTS = "contacts";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_GENDER = "gender";
    private static final String TAG_PHONE = "phone";
    private static final String TAG_PHONE_MOBILE = "mobile";
    private static final String TAG_PHONE_HOME = "home";
    private static final String TAG_PHONE_OFFICE = "office";
     public static JSONObject json  ;

    JSONArray contacts = null;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        final ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

        // Creating JSON Parser instance
        final JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        new Handler().postDelayed(new Runnable() {



            @Override
            public void run() {

                Thread thread = new Thread()
                {
                    @Override
                    public void run() {
                        while(true) {
                            ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

                            // Creating JSON Parser instance
                            JSONParser jParser = new JSONParser();

                            // getting JSON string from URL
                            JSONObject json = jParser.getJSONFromUrl(url);

                            try {
                                // Getting Array of Contacts
                                contacts = json.getJSONArray(TAG_CONTACTS);

                                // looping through All Contacts
                                for(int i = 0; i < contacts.length(); i++){
                                    JSONObject c = contacts.getJSONObject(i);

                                    // Storing each json item in variable
                                    String id = c.getString(TAG_ID);
                                    String name = c.getString(TAG_NAME);
                                    String email = c.getString(TAG_EMAIL);
                                    String address = c.getString(TAG_ADDRESS);
                                    String gender = c.getString(TAG_GENDER);

                                    // Phone number is agin JSON Object
                                    JSONObject phone = c.getJSONObject(TAG_PHONE);
                                    String mobile = phone.getString(TAG_PHONE_MOBILE);
                                    String home = phone.getString(TAG_PHONE_HOME);
                                    String office = phone.getString(TAG_PHONE_OFFICE);


                                    Log.w("ID",id);
                                    Log.w("Name",name);
                                    Log.w("Email",email);
                                    Log.w("Gender",gender);
                                    Log.w("mobile",mobile);
                                    Log.w("home",home);
                                    Log.w("office",office);
                                    Log.w("address",address);



                                    // creating new HashMap
                                    HashMap<String, String> map = new HashMap<String, String>();

                                    // adding each child node to HashMap key => value
                                    map.put(TAG_ID, id);
                                    map.put(TAG_NAME, name);
                                    map.put(TAG_EMAIL, email);
                                    map.put(TAG_PHONE_MOBILE, mobile);

                                    // adding HashList to ArrayList
                                    contactList.add(map);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }


                        }
                    }
                };

                thread.start();




                //
                finish();
            }
        }, 0);





    }

}

这段代码抛出了这个

10-04 13:31:30.402: D/dalvikvm(3043): GC_FOR_ALLOC freed 15K, 4% free 4157K/4292K, paused 39ms, total 48ms
10-04 13:31:30.422: I/dalvikvm-heap(3043): Grow heap (frag case) to 5.637MB for 1536016-byte allocation
10-04 13:31:30.592: D/dalvikvm(3043): GC_FOR_ALLOC freed <1K, 3% free 5656K/5796K, paused 160ms, total 160ms
10-04 13:31:32.562: W/System.err(3043): org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8732 refused
10-04 13:31:32.592: W/System.err(3043):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
10-04 13:31:32.592: W/System.err(3043):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-04 13:31:32.602: W/System.err(3043):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-04 13:31:32.602: W/System.err(3043):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-04 13:31:32.602: W/System.err(3043):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-04 13:31:32.602: W/System.err(3043):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-04 13:31:32.643: W/System.err(3043):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-04 13:31:32.643: W/System.err(3043):     at com.example.usa.JSONParser.getJSONFromUrl(JSONParser.java:38)
10-04 13:31:32.643: W/System.err(3043):     at com.example.usa.Home$1$1.run(Home.java:66)
10-04 13:31:32.643: W/System.err(3043): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 8732): connect failed: ECONNREFUSED (Connection refused)
10-04 13:31:32.682: W/System.err(3043):     at libcore.io.IoBridge.connect(IoBridge.java:114)
10-04 13:31:32.712: W/System.err(3043):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-04 13:31:32.712: W/System.err(3043):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-04 13:31:32.733: W/System.err(3043):     at java.net.Socket.connect(Socket.java:842)
10-04 13:31:32.733: W/System.err(3043):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-04 13:31:32.733: W/System.err(3043):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
10-04 13:31:32.742: W/System.err(3043):     ... 8 more
10-04 13:31:32.772: W/System.err(3043): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
10-04 13:31:32.812: W/System.err(3043):     at libcore.io.Posix.connect(Native Method)
10-04 13:31:32.812: W/System.err(3043):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
10-04 13:31:32.812: W/System.err(3043):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
10-04 13:31:32.812: W/System.err(3043):     at libcore.io.IoBridge.connect(IoBridge.java:112)
10-04 13:31:32.812: W/System.err(3043):     ... 13 more
10-04 13:31:32.854: E/Buffer Error(3043): Error converting result java.lang.NullPointerException: lock == null
10-04 13:31:32.917: E/JSON Parser(3043): Error parsing data org.json.JSONException: End of input at character 0 of 
10-04 13:31:32.917: W/dalvikvm(3043): threadid=11: thread exiting with uncaught exception (group=0x414c4700)
10-04 13:31:32.917: E/AndroidRuntime(3043): FATAL EXCEPTION: Thread-96
10-04 13:31:32.917: E/AndroidRuntime(3043): java.lang.NullPointerException
10-04 13:31:32.917: E/AndroidRuntime(3043):     at com.example.usa.Home$1$1.run(Home.java:70)
10-04 13:31:35.052: I/Choreographer(3043): Skipped 1034 frames!  The application may be doing too much work on its main thread.
10-04 13:31:38.592: I/Choreographer(3043): Skipped 37 frames!  The application may be doing too much work on its main thread.
10-04 13:36:33.602: I/Process(3043): Sending signal. PID: 3043 SIG: 9
4

2 回答 2

1

您正在尝试连接到localhost,这意味着 android 设备本身。您可能在您正在测试的同一本地网络中的桌面上运行您的 Web 服务,因此您需要通过您机器的 IP 访问 Web 服务。

在 Windows 中,打开 cmd.exe,运行ipconfig,然后找到您的 IPv4 地址,然后替换localhost为该地址。

它应该类似于将 192.168.1.2 与您的 IP 放在一起:

private static String url = "http://192.168.1.2:8732/Design_Time_Addresses/WcfServiceLibrary/Service1/GetDataUsingDataContract";
于 2013-10-04T13:48:54.060 回答
0

首先,您必须在 IIS 中部署服务,而不是将 localhost 更改为 10.0.2.2:Yourport

于 2013-12-13T14:00:38.053 回答