0
public class GeoRetrieverService extends Service {

private Thread triggerService;
private final IBinder mBinder = new LocalBinder();
private Intent startingIntent;

@Override
public void onCreate()
{

}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

@Override
public void onDestroy()
{

}   

@Override
public int onStartCommand(final Intent intent,int flags,int startId)
{
    Log.d("geoRetriever", "started geo retriever");
    triggerService = new Thread(new Runnable() {            

        public void run() {
            try{
                // 
                String result = (connect("http://10.0.2.2:8080/GpsServ/GeoRetrieve")//,intent.getStringExtra("nearLeftLat"),
                                                                //                                 intent.getStringExtra("nearLeftLng"),
                                                                //                                 intent.getStringExtra("farRightLat"),
                                                                //                                 intent.getStringExtra("farRightLng")
                                                                //                                 )
                                                                                                   );
                Log.d("geoRetriever", result + " this should be result");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

        //,String topLat,String topLng,String bottomLat,String bottomLng) 
        public String connect(String url)
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpget = new HttpPost(url); 
            HttpResponse response;
            try {
                response = httpclient.execute(httpget);
                //Log.i(TAG,response.getStatusLine().toString());
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instream = entity.getContent();
                    String result= convertStreamToString(instream);
                    instream.close();
                    return result;
                }
            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }
            return null;
        }

        private String convertStreamToString(InputStream is) {
            /*
             * To convert the InputStream to String we use the BufferedReader.readLine()
             * method. We iterate until the BufferedReader return null which means
             * there's no more data to read. Each line will appended to a StringBuilder
             * and returned as String.
             */
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return sb.toString();
        }

    }, "TrackRetrieverThread");

    triggerService.start();

    return START_STICKY;
}
public class LocalBinder extends Binder {
    GeoRetrieverService getService() {
        // Return this instance of LocalService so clients can call public methods
        return GeoRetrieverService.this;
    }       
}

} 

我从 MainActivity 开始这个课程。我确定服务开始了。从浏览器访问 URL 时,我得到了预期的数据。从调试器访问时,我得到空值。

我也尝试过使用 HttpGet 而不是 HttpPost。

服务器的结果是一个 json 字符串。

任何想法为什么我会得到空值?

编辑:它抛出一个

Connection to http://10.0.2.2:8080 refused. 

我猜Tomcat正在断开连接。

我有

<uses-permission android:name="android.permission.INTERNET" />

在清单中。此外,服务器已启动,并在同一地址上响应(使用 localhost 而不是 10.0.2.2)

我不是在调试器内部进行测试,而是在连接到 USB 的设备上进行测试。

4

1 回答 1

0

文档

所以:

  • 使用 10.0.2.2 而不是 localhost 或 127.0.0.1
  • 不要忘记添加到您的清单文件<uses-permission android:name="android.permission.INTERNET" />
于 2013-09-03T13:15:54.397 回答