2012-12-04 50 views
1

最近我正在開發Android應用程序 我想要做的是使用來自.NET WCF的web服務 到我的應用程序連接到Android上使用JSON 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響應= httpClient.execute(請求); 我有搜索整個論壇,但沒有發現任何 我已經加我的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,但它位於同一LAN內的其他PC上 請幫助我

+0

請發佈stacktrace – Mikhaili

回答

0

您正在UI線程上發出網絡請求。這不再被允許,並且會引發一個例外。您需要將代碼移到AsyncTask中。 Google NetworkOnMain有很多例子。

+0

我已經將它移動到AsyncTask,但它給我連接到「http://172.30.2.95:9000」拒絕我已經打開防火牆端口仍然沒有運氣 – lordaeon

+0

首先,檢查您的設備可以通過在設備的瀏覽器中輸入服務URL來訪問服務器。如果這不起作用,則需要解決網絡問題。如果確實有效,請繼續檢查您的應用。你有沒有獲得互聯網許可? – 323go

+0

解決使用AsyncTask謝謝你323go – lordaeon