android json php示例不起作用
我是android JSON新手,从我的服务器数据库表中获取数据时遇到问题。我需要在我的android页面id上显示,在settext上显示名称和地址 但我不是通过JSON从服务器获取数据。以下是我的完整代码: 这是我的JSON文件:android json php示例不起作用,php,android,json,Php,Android,Json,我是android JSON新手,从我的服务器数据库表中获取数据时遇到问题。我需要在我的android页面id上显示,在settext上显示名称和地址 但我不是通过JSON从服务器获取数据。以下是我的完整代码: 这是我的JSON文件: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
以下是我的主要活动:
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.my.data1.JSONParser;
public class Tab1Activity extends Activity {
//URL to get JSON Array
private static String url = "http://app-developments.com/Jasmita/Address- book/Get_product_details.php";
//JSON Node Names
private static final String TAG_Address_Book = "Address_Book";
private static final String TAG_ID = "Id";
private static final String TAG_NAME = "Name";
private static final String TAG_ADD = "Address";
JSONArray Address_Book = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab1);
Toast.makeText(getApplicationContext(), "Welcome", 4000).show();
// Creating new JSON Parser
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting JSON Array
Address_Book = json.getJSONArray(TAG_Address_Book);
JSONObject c = Address_Book.getJSONObject(0);
// Storing JSON item in a Variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String add = c.getString(TAG_ADD);
//Importing TextView
final TextView uid = (TextView)findViewById(R.id.ID1);
final TextView name1 = (TextView)findViewById(R.id.name1);
final TextView email1 = (TextView)findViewById(R.id.add1);
//Set JSON Data in TextView
uid.setText(id);
name1.setText(name);
email1.setText(add);
} catch (JSONException e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(),"Exception is: " +e, 4000).show();
}
}
}
这是我的php文件:
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect1.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
// get a product from products table
$result = mysql_query("SELECT * FROM Address_Book") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$response["Address_Book"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$Address_Book = array();
$Address_Book["Id"] = $row["Id"];
$Address_Book["Name"] = $row["Name"];
$Address_Book["Address"] = $row["Address"];
// push single product into final response array
array_push($response["Address_Book"], $Address_Book);
}
// success // success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
您遗漏了很多信息和代码,但我仍将根据我认为您的问题来回答您的问题: 据我所知,作为数据库GET请求的php文件工作正常,但无法在Android上调用它 当我第一次开始使用Android时,我在GET请求方面也遇到了一些问题,所有我本应该使用教程中包含的类都被弃用了 我终于四处搜索,做了一些有用的东西,并在我的粘贴箱上保存了一个我所做的样本。我将与您分享如何在Android上发出GET请求,您所要做的就是将其应用到您的项目中
public class Main2Activity extends AppCompatActivity {
public static String URL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
URL = "http://The-URL-To-Your-PHP-File";
ConnectToDatabase connect = new ConnectToDatabase();
Thread th = new Thread(connect);
th.start();
}
class ConnectToDatabase implements Runnable {
public void run() {
Log.d("link", URL);
StringBuilder result = new StringBuilder();
try {
URL url = new URL(URL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
urlConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
/*
RESULT - DO WHATEVER WITH IT
*/
Log.d("result", result.toString());
}
}
这应该行得通。如果没有,那么,看在上帝的份上,告诉我们错误信息是什么。您的完整代码在哪里??我看不见。。!!你用卷发吗?你为什么不发布你的代码?等待你的完整代码…你在LogCat中有任何异常吗?没有…它没有连接到数据库我在我的主要活动中应用了它?请澄清我正在使用的eclipse@z.al在执行代码的任何活动上使用它。我当时的名字叫Main2Activity,因为我想不起它的名字。不管怎么说,你可以在你的主要活动中很好地使用它。空指针异常:lock==null我在应用你的code@z.al这可能意味着中的InputStream为空。这可能意味着您的php文件出错,或者您只是错误地实现了我的代码。