I have created a WCF service on localhost and it returns a json on invoke i want to get the json on android client but its not working somehow, i tried getting a json from a sample android tutorial site and that worked but my own localhost WCF service does not seem to work
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);
}
}
this code throws this
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
You're attempting to connect to localhost
, which means the android device itself. You are probably running your web service on a desktop within the same local network that you're testing on, so you will need to access the web service via the IP of your machine.
In windows, open up cmd.exe, run ipconfig
and then find your IPv4 address, then replace localhost
with that.
It should look something like, placing 192.168.1.2 with your IP:
private static String url = "http://192.168.1.2:8732/Design_Time_Addresses/WcfServiceLibrary/Service1/GetDataUsingDataContract";
首先,您必须在 IIS 中部署您的服务,而不是将 localhost 更改为 10.0.2.2:Yourport
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.