2012-04-10 122 views
0

對不起,麻煩您了,因爲我是Android編程的新手,並且在嘗試檢索我的發送並從本地主機服務器接收響應時遇到以下問題。Android Json HTTP Post,無法解析數據

當我嘗試首先啓動它時,該程序似乎會自動關閉。但實施後

StrictMode.ThreadPolicy policy = new StrictMode. 
ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 

該程序能夠運行,但數據不分析。

我已經在我的android Manifest腳本中允許Internet權限。

我的Android代碼

package kx.practice; 

import android.app.Activity; 
import android.os.Bundle; 
import android.os.StrictMode; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.widget.TextView; 



public class JsonHttpPractice2Activity extends Activity { 

TextView tv; 
String text; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

StrictMode.ThreadPolicy policy = new StrictMode. 
ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 

tv = (TextView)findViewById(R.id.textView1); 
text = ""; 

try { 
postData(); 
} catch (JSONException e) { 
// TODO Auto-generated catch block 
System.out.println("Error in JSON Exception 1"); 
e.printStackTrace(); 
} 
} 

public void postData() throws JSONException{ 
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://localhost/phpWebservice/AndroidTest.php"); 
JSONObject json = new JSONObject(); 

try { 
// JSON data: 
json.put("name", "Fahmi Rahman"); 
json.put("position", "sysdev"); 

JSONArray postjson=new JSONArray(); 
postjson.put(json); 

// Post the data: 
httppost.setHeader("json",json.toString()); 
httppost.getParams().setParameter("jsonpost",postjson); 

// Execute HTTP Post Request 
System.out.print(json); 
HttpResponse response = httpclient.execute(httppost); 

// for JSON: 
if(response != null) 
{ 
InputStream is = response.getEntity().getContent(); 

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(); 
} 
} 
text = sb.toString(); 
System.out.println("This is my text" +text); 
} 

tv.setText(text); 

}catch (ClientProtocolException e) { 
System.out.println("Error in JSON Exception 2"); 
// TODO Auto-generated catch block 
} catch (IOException e) { 
System.out.println("Error in JSON Exception 3"); 
// TODO Auto-generated catch block 
} 
} 
} 

最後,我的PHP代碼

<?php 
$json = $_SERVER['HTTP_JSON']; 
echo "JSON: \n"; 
echo "--------------\n"; 
var_dump($json); 
echo "\n\n"; 

$data = json_decode($json); 
echo "Array: \n"; 
echo "--------------\n"; 
var_dump($data); 
echo "\n\n"; 

$name = $data->name; 
$pos = $data->position; 
echo "Result: \n"; 
echo "--------------\n"; 
echo "Name  : ".$name."\n Position : ".$pos; 
?> 

順便說一句,我從一個在線網站這些代碼。但是,如果這些代碼能夠工作,我應該能夠將其實施到我的項目中。

回答

1

您正試圖在主線程上訪問網絡。這是一個very bad idea。您需要在單獨的線程中進行網絡訪問。鏈接的文章爲此提供了幾條指導原則。

如果在將網絡訪問權移動到其他線程後仍然存在問題,請隨時回來並提出更多問題。

(您的應用程序被殺害的原因是因爲Android系統發現它沒有迴應了一段時間,因爲它正在等待網絡流量。)

+0

嗨,感謝您的feedback.Actually我嘗試運行它在不同的線程,但它仍然無法正常工作。我發現了這個錯誤導致它是android不接受本地主機,但需要一個IP發送變量to.Thanks =) – user1324336 2012-04-12 11:38:43