1

最近我正在开发 Android 应用程序,我想做的是将 .net WCF 中的 Web 服务消费到我的应用程序中

这是我的客户代码

import java.io.InputStream;<br>
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;<br>
import org.apache.http.HttpResponse;<br>
import org.apache.http.client.methods.HttpGet;<br>
import org.apache.http.impl.client.DefaultHttpClient;<br>
import org.json.JSONObject;<br>

import android.R.string;<br>
import android.os.Bundle;<br>
import android.app.Activity;<br>
import android.view.Menu;<br>
import android.view.View;<br>
import android.widget.TextView;<br>


public class MyPamIndex extends Activity {

    private final static String SERVICE_URI = "http://172.30.2.95:9000/JSON/MyPam.svc";
    private TextView NabVals;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_pam_index);


            NabVals = (TextView)findViewById(R.id.textView2);
    }
    public void OnRefreshClick(View button)
    {
         try {
             // Send GET request to <service>/GetVehicle/<plate>
             DefaultHttpClient httpClient = new DefaultHttpClient();
             HttpGet request = new HttpGet(SERVICE_URI + "/ProductID/" + "1");

             request.setHeader("Accept", "application/json");
             request.setHeader("Content-type", "application/json");

             HttpResponse response = httpClient.execute(request);
             HttpEntity responseEntity = response.getEntity();

             // Read response data into buffer
             char[] buffer = new char[(int)responseEntity.getContentLength()];
             InputStream stream = responseEntity.getContent();
             InputStreamReader reader = new InputStreamReader(stream);
             reader.read(buffer);
             stream.close();

             JSONObject vehicle = new JSONObject(new String(buffer));
             NabVals.setText(vehicle.getString("NabValue"));
             // Populate text fields


         } catch (Exception e) {
             e.printStackTrace();

         }
    }

当我运行应用程序时,它在 HttpResponse response = httpClient.execute(request); 中有错误 我已经搜索了整个论坛但什么也没找到我已经添加了我的 android 清单文件但仍然无法工作请帮助我

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.core.mypam"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="anroid.permission.INTERNET"></uses-permission>
    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.core.mypam.MyPamIndex"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

“http://172.30.2.95:9000”不是我的机器IP,而是位于同一局域网内的其他PC上请帮助我

4

1 回答 1

0

您正在 UI 线程上发出网络请求。这不再被允许,并且会引发异常。您需要将代码移动到 AsyncTask 中。谷歌NetworkOnMain有很多例子。

于 2012-12-04T07:56:31.757 回答