0

我试图通过获取请求使用 AsyncTask<> 从一个简单的 android 应用程序发送一些参数,但是当我在 servlet 上获取参数时,这些参数为空!而且我不明白为什么......,logcat没有显示任何错误,所以我找不到原因......这是我的主要活动:

package com.theopentutorials.android;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

//the main activity
public class HttpGetServletActivity extends Activity implements OnClickListener {
Button button; // gui part
TextView outputText;
EditText titulli;
EditText ingredientet;
EditText receta;

public static String getTitulliS() {
    return titulliS;
}

// setters and getters of the url parameters
public static void setTitulliS(String titulliS) {
    HttpGetServletActivity.titulliS = titulliS;
}

public static String getIngredientetS() {
    return ingredientetS;
}

public static void setIngredientetS(String ingredientetS) {
    HttpGetServletActivity.ingredientetS = ingredientetS;
}

public static String getRecetaS() {
    return recetaS;
}

public static void setRecetaS(String recetaS) {
    HttpGetServletActivity.recetaS = recetaS;
}

public static String getURL() {
    return URL;
}

public static void setURL(String uRL) {
    URL = uRL;
}

// the parameters i would like to send
static String titulliS;
static String ingredientetS;
static String recetaS;
static String URL = "http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet?"
        + getTitulliS() + getIngredientetS() + getRecetaS();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewsById();

    button.setOnClickListener(this);
}

private void findViewsById() {
    button = (Button) findViewById(R.id.button);
    outputText = (TextView) findViewById(R.id.outputTxt);
    titulli = (EditText) findViewById(R.id.editText1);
    ingredientet = (EditText) findViewById(R.id.editText2);
    receta = (EditText) findViewById(R.id.editText2);
    setTitulliS("titulli=" + titulli.getText().toString() + "&");
    setIngredientetS("ingredientet=" + ingredientet.getText().toString()
            + "&");
    setRecetaS("receta=" + receta.getText().toString());

}

public void onClick(View view) {

    GetXMLTask task = new GetXMLTask();
    task.execute(new String[] { URL });
}

private class GetXMLTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String output = null;
        for (String url : urls) {
            output = getOutputFromUrl(url);
        }
        return output;
    }

    private String getOutputFromUrl(String url) {
        StringBuffer output = new StringBuffer("");
        try {
            InputStream stream = getHttpConnection(url);
            BufferedReader buffer = new BufferedReader(
                    new InputStreamReader(stream));
            String s = "";
            while ((s = buffer.readLine()) != null)
                output.append(s);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return output.toString();
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }

    @Override
    protected void onPostExecute(String output) {
        outputText.setText(output);
    }
}
}

这是我的简单 servlet:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public HelloWorldServlet() {
    super();
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
       String titulli =  request.getParameter("titulli");
      String ingredientet =  request.getParameter("ingredientet");
       String receta =  request.getParameter("receta");
        PrintWriter out = response.getWriter();
        out.println(titulli+ingredientet+receta);
        System.out.println(titulli+ingredientet+receta);
}
}

有人知道吗?提前谢谢你!

4

1 回答 1

1

对我来说,看起来好像您只在 中为输入字段设置参数扩展名,例如onCreate(),当您真的必须这样做时onClick()

我还有一些关于“为什么是静态的?”的评论。等等,但你没有要求。;)

于 2013-01-30T15:44:50.950 回答