Android AsyncTask Url无法解析

时间:2013-09-12 04:49:18

标签: php android json android-asynctask httprequest

我正在尝试将参数添加到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();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

修改 在这一行之后,

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

添加:

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

<强> EDIT2:

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

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

AsyncTask代替DBURL使用MainActivity.DBURL