though the code is a little long, most of them are very simple IMO. now im writing an android app that posts data to a website and displays the returned HTML code to the TextView.First i wrote a simple java POST method, it works perfect in my computer(on linux, post username and password to http://forum.xda-developers.com/login.php, and it returned the HTML successfully, though which prompts me that the username and password is incorrect):
package com.app.main;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class Connection
{
String returnText = "";
String returnLine;
//data which is about to be posted
public String data = "vb_login_username=test&vb_login_password&securitytoken=guest&do=login";
public String returnHTML()
{
try {
URL url = new URL("http://forum.xda-developers.com/login.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while((returnLine = rd.readLine()) != null) {
returnText += returnLine;
}
wr.close();
rd.close();
return returnText;
} catch (Exception e) {
return "ERROR: "+e.getMessage();
}
}
}
i tested the code using:
Connection net_start = new Connection();
System.out.println(net_start.returnHTML());
perfect. HTML returned successfully, then i go to write an android app, which is very simple, only having a button and an TextView on it. when i press the button, the TextView will display the returned HTML if successfully. Here is the source code of the main Activity.java
package com.app.main;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class AndroidTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView displayResult = (TextView)findViewById(R.id.displayResult);
Button login = (Button)findViewById(R.id.login);
//set dispalyResult scrollable
displayResult.setMovementMethod(new ScrollingMovementMethod());
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Connection net_start = new Connection();
displayResult.setText(net_start.returnHTML());
}
});
}
}
very simple code, the only difference from that above is that i set the returned HTML display on the TextView instead of the command line. but i failed. According to debugging, i found the app throws an Exception at that line in Connection.java: OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());, the catched e said that: java.net.UnknownHostException: forum.xda-developers.com, but i indeed can login the xda-developers website on my cellphone(i debug the app using adb on my phone), what’s the matter with that? cannot android use URLConnection? any help appreciated 🙂
Is it available?
in your android application’s manifest file..
Your code works fine in my case:
The result of
returnTextis :