我寫了下面的代碼,從一個PHP文件中獲得一個簡單的消息,它向我發送了一個json對象,我的代碼中沒有錯誤,PHP代碼運行良好,請你幫我找到是我的代碼:)從JSon得到消息
Java代碼事項:
package com.example.httpclient;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView myListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This is our textView element
myListView = (TextView) findViewById(R.id.textView1);
//Lets try to connect
try{
//Create a new client object
HttpClient httpclient = new DefaultHttpClient();
//Now post to your demo URL
HttpPost httppost = new HttpPost("http://goldengym.ma/test/test1.php");
//Execute the post and get the response
HttpResponse response = httpclient.execute(httppost);
//Get the message from the response
HttpEntity entity = response.getEntity();
//Get the content of the message
InputStream webs = entity.getContent();
//Convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"), 8);
//Read one line of the response
myListView.setText(reader.readLine());
//Slow our inputStream
webs.close();
}catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
}catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString());
}
}catch (Exception e){
Log.e("log_tag", "Error " + e.toString());
}
}
}
而且有PHP代碼:
<?php
$result = 'TEST WORKED, WE GOT CONNECTION';
print json_encode($result);
?>
而且有我的logcat:
在主線程取得03-03 12:35:02.918: I/ActivityManager(1995): Force stopping package com.example.httpclient uid=10118
03-03 12:35:04.518: W/PackageManager(1995): Code path for pkg : com.example.httpclient changing from /data/app/com.example.httpclient-2.apk to /data/app/com.example.httpclient-1.apk
03-03 12:35:04.518: I/ActivityManager(1995): Force stopping package com.example.httpclient uid=10118
03-03 12:35:04.518: W/PackageManager(1995): Resource path for pkg : com.example.httpclient changing from /data/app/com.example.httpclient-2.apk to /data/app/com.example.httpclient-1.apk
03-03 12:35:05.218: I/ActivityManager(1995): Force stopping package com.example.httpclient uid=10118
03-03 12:35:05.493: D/Launcher.LauncherModel(5871): --> package:com.example.httpclient
03-03 12:35:05.723: D/Launcher.LauncherModel(5871): --> update package com.example.httpclient
03-03 12:35:05.723: D/Launcher.LauncherModel(5871): --> package:com.example.httpclient
03-03 12:35:05.913: V/BackupManagerService(1995): updatePackageParticipantsLocked: com.example.httpclient
03-03 12:35:06.188: D/PackageBroadcastService(2465): Received broadcast action=android.intent.action.PACKAGE_REMOVED and uri=com.example.httpclient
03-03 12:35:06.523: V/BackupManagerService(1995): updatePackageParticipantsLocked: com.example.httpclient
03-03 12:35:07.598: W/DeepLinking(3458): no deep link install data found for com.example.httpclient
03-03 12:35:07.663: D/PackageBroadcastService(2465): Received broadcast action=android.intent.action.PACKAGE_ADDED and uri=com.example.httpclient
03-03 12:35:07.703: D/PackageAddedReceiver(2234): package added com.example.httpclient
03-03 12:35:08.048: D/PackageBroadcastService(2465): Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=com.example.httpclient
讓你有沒有在logcat的,但你的TextView是空afre要求? – nikis
感謝您的回覆,我的日誌中沒有任何錯誤,textView只是不顯示任何東西(空) –
你可以發佈你的logtcat –