如何从Java Servlet

时间:2016-01-29 15:19:48

标签: java android google-app-engine servlets gson

我已成功完成Android Studio Java servlets模块here。我正在尝试更改从服务器收到的响应并将其显示为吐司。我设置了这个例子并收到一个说“你好”+名字的祝酒词。然后我更改了res.getWriter()方法中的文本,如下所示:

public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    resp.setContentType("text/plain");
    resp.getWriter().println("Please use the form to POST to this url");
}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String name = req.getParameter("name");
    resp.setContentType("text/plain");
    if(name == null) {
        resp.getWriter().println("Please enter a name");
    }
    resp.getWriter().println("A test to change text" + name);
  }
}

这是我的MainActivity:

public class MainActivity extends AppCompatActivity {

TextView mNumberCounter;
Button mIncrease;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mNumberCounter = (TextView) findViewById(R.id.number_counter);
    mIncrease = (Button) findViewById(R.id.increase_button);


    mIncrease.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new ServletPostAsyncTask().execute(new Pair<Context, String>(getApplicationContext(), mNumberCounter.getText().toString()));
        }
    });
  }
}

和ServletPostAsyncTask类:

public class ServletPostAsyncTask extends AsyncTask<android.support.v4.util.Pair<Context, String>, Void, String> {
private Context context;

@Override
    protected String doInBackground(android.support.v4.util.Pair<Context, String>... params) {
    context = params[0].first;
    String name = params[0].second;

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://servlet-example-1198.appspot.com/hello");
    try {
        // Add name data to request
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("name", name));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpClient.execute(httpPost);
        if (response.getStatusLine().getStatusCode() == 200) {
            return EntityUtils.toString(response.getEntity());
        }
        return "Error: " + response.getStatusLine().getStatusCode() + " " + response.getStatusLine().getReasonPhrase();

    } catch (ClientProtocolException e) {
        return e.getMessage();
    } catch (IOException e) {
        return e.getMessage();
    }
}

@Override
protected void onPostExecute(String result) {
    Toast.makeText(context, result, Toast.LENGTH_LONG).show();

  }
}

不幸的是,我仍然收到“Hello”+我在TextView中的数字1的响应,当我希望它现在说“更改文本的测试:我正在研究如何更改此文本并且还改变服务器响应类型,即如果我想接收布尔值或int而不是简单文本。据我所知,这是在resp.setContentType()中处理的,但我正在寻找一些指导。

1 个答案:

答案 0 :(得分:0)

有趣的是,也许有人可以帮助解释这种差异。当我只是单击我的应用程序模块的绿色箭头时,它不会推送到服务器,但Build&gt;将模块部署到App Engine解决了我的问题。不确定是否存在差异..

enter image description here