我是 android 新手,我正在开发 android 应用程序的项目下工作。我想知道上传 PHP 脚本的步骤,该脚本是连接和对存储在 Web 服务器中的远程数据库执行操作所需的。如果有人知道在服务器上放置 PHP 脚本的位置以及我的 PHP 脚本需要进行哪些更改,请纠正我。
当我在模拟器上执行下面的 java 代码时,它只会给我这样的错误:“解析数据时出错 org.json.JSONException:值
这意味着 PHP 脚本不在数据库上执行,即为什么它只直接返回脚本。而且因为它直接返回字符串形式的脚本,所以我声明的 JSONArray 无法将它从字符串转换。
请任何知道这个问题的人,然后在我犯错误的地方纠正。
先感谢您,
PHP 脚本:
<?php
$dbHost = '******';
$dbUser = '***';
$dbPass = '****';
$dbName = '*****';
if (!mysql_connect($dbHost, $dbUser, $dbPass))
{
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong connecting to the database: '.mysql_error());
}
else if (!mysql_select_db($dbName))
{
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong selecting the database: '.mysql_error());
}
$query = "SELECT *
FROM usermaster WHERE
UserName = '".mysql_real_escape_string($_REQUEST['username'])."'";
if (!$result = mysql_query($query))
{
header('HTTP/1.1 500 Internal Server Error');
exit('Oh No! Something went wrong with the query: '.mysql_error());
}
while ($row = mysql_fetch_assoc($result))
{
$output[] = $e;
}
mysql_close();
exit(json_encode($output));
?>
Java 代码:
package com.taseen.apps;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class TestExternalDatabaseActivity extends Activity {
/** Called when the activity is first created. */
TextView resultView;
String s = null;
String result = null;
InputStream isr = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new RemoteConnectivity().execute();
}
private class RemoteConnectivity extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... arg0) {
resultView = (TextView) findViewById(R.id.result);
InputStream is = null;
String result = "";
// the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", "paresh"));
// http post
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://krishisense.org/PHPScript/getlogin.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
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();
result = sb.toString();
}
catch (Exception e)
{
Log.e("log_tag", "Error converting result " + e.toString());
}
// parse json data
try
{
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++)
{
JSONObject json = jArray.getJSONObject(i);
s = s + "User Name : " + json.getString("UserName") + "\n"
+ "Password : " + json.getInt("Password") + "\n"
+ "User Type : " + json.getString("LoginType")
+ "\n\n";
}
}
catch (JSONException e)
{
Log.e("log_tag", "Error parsing data " + e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result1)
{
resultView.setText(s);
}
}
}