Android json获取问题WCF服务

时间:2013-10-04 13:45:54

标签: java android json wcf android-emulator

我已经在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

2 个答案:

答案 0 :(得分:1)

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

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

看起来应该是这样的,用你的IP放置192.168.1.2:

private static String url = "http://192.168.1.2:8732/Design_Time_Addresses/WcfServiceLibrary/Service1/GetDataUsingDataContract";

答案 1 :(得分:0)

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