相关疑难解决方法(0)

Android Honeycomb:Fragment无法启动AsyncTask?

我之前遇到过这个错误,但认为严格模​​式系统有些错误.然而,它显然是正确的,因为我现在遗憾地发现了.:(

我的程序由一个Activity和一堆Fragments组成.我有一个NetworkWorker片段,它启动这样的URL请求:

public void startURLRequest(Fragment target, String url, String message)
{

    if (asyncTask != null) asyncTask.cancel(true);
    asyncTask = new FragmentHttpHelper(url, message, target);
    asyncTask.doInBackground();

    return;
}
Run Code Online (Sandbox Code Playgroud)

FragmentHttpHelper是从AsyncTask派生的自定义内部类:

private class FragmentHttpHelper extends AsyncTask<Void, Void, String>
{
    //...

    @Override
    protected String doInBackground(Void... params)
    {

        if (CheckInternet())
        {
            try
            {
                URL myURL = new URL(url);
                httpClient = new DefaultHttpClient();

                if (this.message == null)
                {
                    httpRequest = new HttpGet(myURL.toExternalForm());
                }
                else
                {
                    httpRequest = new HttpPost(myURL.toExternalForm());

                    HttpEntity myEntity = new StringEntity(message, "UTF-8");
                    ((HttpPost) …
Run Code Online (Sandbox Code Playgroud)

android android-asynctask android-fragments android-3.0-honeycomb

3
推荐指数
1
解决办法
2842
查看次数

Android:无法发送http帖子

我一直在试图弄清楚如何在Android中发送post方法.这就是我的代码的样子:

public class HomeActivity extends Activity implements OnClickListener {

    private TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        textView = (TextView) findViewById(R.id.text);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {    
        HttpPost httpMethod = new HttpPost("http://www.example.com/");
        httpMethod.addHeader("Accept", "text/html");
        httpMethod.addHeader("Content-Type", "application/xml");


        AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
        String result = null;
        try {
            HttpResponse response = client.execute(httpMethod);
            textView.setText(response.toString());

            HttpEntity entity = response.getEntity();

            Log.i(HomeActivity.class.toString(), result);
            textView.setText("Invoked webservice");
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(HomeActivity.class.toString(), e.getMessage());
            textView.setText("Something wrong:" + e.getMessage()); …
Run Code Online (Sandbox Code Playgroud)

java android http-post

2
推荐指数
2
解决办法
2万
查看次数