0

我正在尝试向 JSONPARSER 对象的 makeHttpRequest 添加一个参数。我觉得这与类继承有关,但没有运气。我试图让 DBURL 成为一个参数,以便我可以将数据发布到数据库。我在 Register 类上关注这个 [tutorial][1]。此外,在 onPostExecute 方法中, MainActivity.this; 将不起作用,第一个参数只有一个空值。任何想法都会很棒,如果您需要更多信息,请告诉我。

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity
{

    private static final String DBURL = "http:/demo.php";

    //obtain access to jsonparser class functions
    JSONParser jsonParser = new JSONParser();

    //local initialization of webview
    private WebView webview;
    Context context = this;


    //storage of collected emails, unlimited size
    ArrayList<String> emailsCollection = new ArrayList<String>();

    @SuppressWarnings("rawtypes")
    //to later remove duplicate arraylist items, see solution below
    HashSet hashedEmails = new HashSet();

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //progress dialog message while loading app resources
        final ProgressDialog pd = ProgressDialog.show(this, "", "Starting App",true);


        //webview specific settings
        webview = (WebView) findViewById(R.id.webview1);
        webview.getSettings().setJavaScriptEnabled(true);
     //   webview.getSettings().setSupportZoom(true);  
     //   webview.getSettings().setBuiltInZoomControls(true);
     //   webview.getSettings().setLoadWithOverviewMode(true);
     //   webview.getSettings().setUseWideViewPort(true);
        webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

    //creating an instance of the webview client that supports a progress dialog    
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                if(pd.isShowing()&&pd!=null)
                {
                    pd.dismiss();
                }
            }
        });

        //url to load in the webview
        webview.loadUrl("http://google.com/");
        //overrides and sets title of app title bar despite original activity name
        setTitle("");



    }

    protected void onPostExecute(String file_url)
    {
        if(file_url !=null)
        {
            Toast.makeText(null, file_url, Toast.LENGTH_LONG).show();
        }
    }
}
4

2 回答 2

2

doInBackground()方法的目的是将作业的结果(在工作线程上执行)传递给onPostExecute,以便在 UI 线程上处理结果。如果您想要更新用户界面以响应成功的作业运行,这是必需的。

于 2013-09-12T05:05:22.797 回答
1

file_url将始终为空,postExecute()因为您没有在doInBackground()方法中返回任何内容。解析您的 JSON 响应并在您的 catch 块之前返回它。

编辑: 在这行之后,

 Log.d("JSON Status: ", json.toString());

添加这个:

    if (json != null){
        return json.toString();
    }

编辑2:

您无法访问DBURL,因为它位于不同的Class. 将其更改为公开:

 public static final String DBURL = "http:/demo.php";

而从你的AsyncTask而不是DBURL使用MainActivity.DBURL

于 2013-09-12T04:54:46.733 回答