英文:
Error: "Cannot resolve method 'makeHttpRequest' in 'JSONParser'"
问题
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class EditProductActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_user_detail = "https://localhost/android_connect/get_user_details.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
onStart();
}
public void log(View view){
EditText username=(EditText) findViewById(R.id.username);
EditText password=(EditText) findViewById(R.id.password);
String account_username=username.getText().toString();
String account_password=password.getText().toString();
new getUserRole().execute(account_username, account_password);
//todo
}
/**
* Background Async Task to Get complete user details
* */
class getUserRole extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditProductActivity.this);
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting user details in background thread
* */
protected String doInBackground(final String... params) {
int success;
String result=null;
try {
// getting user details by making HTTP request
// Note that user details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(url_user_detail, "GET", params); // <----------
// check your log for json response
Log.d("Single User Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received user details
JSONArray roleObj = json
.getJSONArray("role"); // JSON Array
// get first user object from JSON Array
JSONObject role = roleObj.getJSONObject(0);
result= role.toString();
}else{
// user not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
}
英文:
I'm trying to use the 'JSONParser' method 'makeHttpRequest' in my Android app to get some json obejcts from a php script which get some data from a mySql db.
Trying to build the project I'm getting the error "Cannot resolve method 'makeHttpRequest' in 'JSONParser'".
I don't know how to fix it, I searched online for some solutions but found nothing that could fix it.
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class EditProductActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_user_detail = "https://localhost/android_connect/get_user_details.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
onStart();
}
public void log(View view){
EditText username=(EditText) findViewById(R.id.username);
EditText password=(EditText) findViewById(R.id.password);
String account_username=username.getText().toString();
String account_password=password.getText().toString();
new getUserRole().execute(account_username, account_password);
//todo
}
/**
* Background Async Task to Get complete userj details
* */
class getUserRole extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditProductActivity.this);
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting user details in background thread
* */
protected String doInBackground(final String... params) {
int success;
String result=null;
try {
// getting user details by making HTTP request
// Note that user details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(url_user_detail, "GET", params); // <----------
// check your log for json response
Log.d("Single User Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received user details
JSONArray roleObj = json
.getJSONArray("role"); // JSON Array
// get first user object from JSON Array
JSONObject role = roleObj.getJSONObject(0);
result= role.toString();
}else{
// user not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
} ```
</details>
# 答案1
**得分**: 0
以下是翻译好的内容:
如何导入json-simple库 - 是否与以下内容相同:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
您可以在[源代码][1]中看到此方法不存在。如果您正在遵循某个教程,请确保包含正确的依赖项。
[1]: https://github.com/fangyidong/json-simple/blob/master/src/main/java/org/json/simple/parser/JSONParser.java
<details>
<summary>英文:</summary>
How do you import json-simple - is it the same one as this:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
You can see in [the source][1] that this method does not exist. If you're following some tutorial, make sure to include the correct dependencies
[1]: https://github.com/fangyidong/json-simple/blob/master/src/main/java/org/json/simple/parser/JSONParser.java
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论