私はAndroidが初めてで、Androidアプリケーションをローカルサーバーphpmyadminに接続しようとしています。PHP ページ (JSON を使用) を介した Android-MySql の相互作用に関するチュートリアルを見たことがありますが、出力に問題があります (たとえば、JSON から取得したものを String に変換するにはどうすればよいですか?)。問題をよりよく理解するために、これは私のコードです:
JSONParser.java
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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;
}
}
これは私の PHP ファイル「get_product.php」です (DB 接続は正常に動作します)。
<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
if (isset($_GET["ID"])) {
$ID_work = $_GET['ID'];
$result = mysql_query("SELECT * FROM PRODUCTS WHERE ID = $ID");
if (!empty($result)) {
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["ID"] = $result["ID"];
$product["title"] = $result["title"];
$product["description"] = $result["description"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
マニフェストにインターネット アクセス許可を挿入し、アクティビティに次を挿入します。
private static String url = "http://IP_ADDRESS/connect/get_product.php";
しかし、文字列で取得したタイトルと説明をどのように保存できますか?
本当にありがとうございました!
@Sajmon: これは LogCat です
05-13 19:53:32.322: E/JSON Parser(10336): Error parsing data org.json.JSONException: Value <HTML><HEAD><TITLE> of type java.lang.String cannot be converted to JSONObject
05-13 19:53:32.322: D/AndroidRuntime(10336): Shutting down VM
05-13 19:53:32.322: W/dalvikvm(10336): threadid=1: thread exiting with uncaught exception (group=0x4001d578)
05-13 19:53:32.332: E/AndroidRuntime(10336): FATAL EXCEPTION: main
05-13 19:53:32.332: E/AndroidRuntime(10336): java.lang.RuntimeException: Unable to start activity ComponentInfo{package/package.MyActivity}: java.lang.NullPointerException
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.app.ActivityThread.access$1500(ActivityThread.java:121)
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.os.Looper.loop(Looper.java:138)
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.app.ActivityThread.main(ActivityThread.java:3701)
05-13 19:53:32.332: E/AndroidRuntime(10336): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 19:53:32.332: E/AndroidRuntime(10336): at java.lang.reflect.Method.invoke(Method.java:507)
05-13 19:53:32.332: E/AndroidRuntime(10336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
05-13 19:53:32.332: E/AndroidRuntime(10336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
05-13 19:53:32.332: E/AndroidRuntime(10336): at dalvik.system.NativeStart.main(Native Method)
05-13 19:53:32.332: E/AndroidRuntime(10336): Caused by: java.lang.NullPointerException
05-13 19:53:32.332: E/AndroidRuntime(10336): at package.MyActivity.onCreate(MyActivity.java:158)
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 19:53:32.332: E/AndroidRuntime(10336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
05-13 19:53:32.332: E/AndroidRuntime(10336): ... 11 more
158行目は次のとおりです。
String title = obj.getString("title");