错误: 在’JSONParser’中无法解析方法’makeHttpRequest’

huangapple 未分类评论90阅读模式
英文:

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 = &quot;https://localhost/android_connect/get_user_details.php&quot;;

    // JSON Node names
    private static final String TAG_SUCCESS = &quot;success&quot;;

    @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&lt;String, String, String&gt; {

        /**
         * 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, &quot;GET&quot;, params); //       &lt;----------
                // check your log for json response
                Log.d(&quot;Single User Details&quot;, json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully received user details
                    JSONArray roleObj = json
                            .getJSONArray(&quot;role&quot;); // 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:

    &lt;dependency&gt;
	  &lt;groupId&gt;com.googlecode.json-simple&lt;/groupId&gt;
	  &lt;artifactId&gt;json-simple&lt;/artifactId&gt;
	  &lt;version&gt;1.1.1&lt;/version&gt;
    &lt;/dependency&gt;

You can see in [the source][1] that this method does not exist. If you&#39;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>



huangapple
  • 本文由 发表于 2020年8月15日 03:10:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/63418789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定