遵循本教程:http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ 但我正在修改并将其用于个人用途。
首先让我们从错误开始:
Error parsing data org.json.JSONException: Value create_product.php of type java.lang.String cannot be converted to JSONObject
VM exiting with result code 0, cleanup skipped.
^退出仅仅是因为我放了一个简单的if(jsonobject == null){System.exit(0);}
排除事情: 当我使用“http://api.androidhive.info/android_connect/create_product.php”作为我的testURL而不是我自己的“ttp://192.168.1.8/HypeServer/android_connect/create_product.php”时,json正确解析并且回复的参数不足 - 这是正确的输出。
在这种情况下,唯一似乎正在改变的变量是服务器地址 - 因为这实际上是我必须改变以在功能模块和非运行模块之间切换的唯一线路。所以我认为问题在于我的服务器配置设置。
我的笔记本电脑和网络上的另一台计算机都可以访问我的服务器。我将链接完全复制到我的代码中。运行php脚本也可以使用gui,甚至不是android studio中的java代码。我正在使用与我在同一个wifi网络上的手机,似乎从我的服务器抓取至少某种形式的数据,它似乎没有运行脚本。
版本: LAMP - Ubuntu(14.04) - Apache(2.4.7) - MySQL(5.5.37) - PHP(5.5.9)
我自学了很多,所以很可能我错过了一些非常明显的概念,但我只是难过。另一方面,如果我正在做其他非常糟糕的事情,我很想知道。
以下是所有相关代码:
public class ServerConnectActivity extends ActionBarActivity{
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
// url to get all products list
private static String url_create_user = "http://192.168.1.8/HypeServer/android_connect/create_product.php";
//"http://192.168.1.8/HypeServer/android_connect/create_account.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
//list of data to be added
protected List<NameValuePair> params = new ArrayList<NameValuePair>();
protected void onCreate(Bundle b) {
super.onCreate(b);
}
public void setData() {
params.add(new BasicNameValuePair("name", "Ev0xFrost"));
params.add(new BasicNameValuePair("password", "lamda"));
}
public void setHttp(String s) {
s = url_create_user;
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
return true;
}
/**
* Background Async Task to Create new product
*/
protected class CreateNewGeneric extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
setData();
pDialog = new ProgressDialog(ServerConnectActivity.this);
pDialog.setMessage("Logging in..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
*/
protected String doInBackground(String... args) {
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_user,
"POST", params);
// check log cat fro response
if (json == null) {
System.exit(0);
}
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(ServerConnectActivity.this, TimelineActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
//TODO: tell users there was a problem connecting with the servers?
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
-
public class MainActivity extends ServerConnectActivity {
public final static String EXTRA_MESSAGE = "com.Hype.Hype.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//disables the action bar
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
Button btnCreateUser = (Button) findViewById(R.id.button3);
// button click event
btnCreateUser.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewGeneric().execute();
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
default:
return super.onOptionsItemSelected(item);
}
}
}
- 他的PHP脚本是链接的 - 我的版本是一个精确的副本 - 实际上它是用create_user.php脚本编写的,但由于两个脚本都产生了相同的错误,我继续前进并决定排除错误的PHP编码。