I have successfully completed the Android Studio Java servlets module here . I am trying to change the response I receive from the server and display it as a toast. I set up the example and received a toast that said "Hello" + name. I then changed the text in the res.getWriter() method, as shown below:
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);
}
}
Here is my 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()));
}
});
}
}
And the ServletPostAsyncTask class:
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();
}
}
Unfortunately, I am still receiving the response of "Hello" + the number 1 that I have in my TextView, when I expect it to now say "A test to change text.: I am looking on how to change this text and also change the server response types, ie if I want to receive a boolean or a int instead of simple text. From what I understand, this is handled in the resp.setContentType(), but I am looking for some guidance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.