У меня простой проект, но я не знаю, почему результат не показывает результат.
Я получаю код от http://androidexample.com/How_To_Make_HTTP_POST_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=64&aaid=89
Я хочу, чтобы результат, подобный картинке 3, на этом веб-сайте
это код Java
public class MainActivity extends Activity {
TextView content;
EditText fname, email, login, pass;
String Name, Email, Login, Pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
content = (TextView)findViewById( R.id.content );
fname = (EditText)findViewById(R.id.name);
email = (EditText)findViewById(R.id.email);
login = (EditText)findViewById(R.id.loginname);
pass = (EditText)findViewById(R.id.password);
Button saveme=(Button)findViewById(R.id.save);
saveme.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
try{
// CALL GetText method to make post method call
GetText();
}
catch(Exception ex)
{
content.setText(" url exeption! " );
}
}
});
}
public void GetText() throws UnsupportedEncodingException
{
// Get user defined values
Name = fname.getText().toString();
Email = email.getText().toString();
Login = login.getText().toString();
Pass = pass.getText().toString();
// Create data variable for sent values to server
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode(Name, "UTF-8");
data += "&" + URLEncoder.encode("email", "UTF-8") + "="
+ URLEncoder.encode(Email, "UTF-8");
data += "&" + URLEncoder.encode("user", "UTF-8")
+ "=" + URLEncoder.encode(Login, "UTF-8");
data += "&" + URLEncoder.encode("pass", "UTF-8")
+ "=" + URLEncoder.encode(Pass, "UTF-8");
String text = "";
BufferedReader reader=null;
// Send data
try
{
// Defined URL where to send data
URL url = new URL("http://angkotfinder.16mb.com/cobaserver.php");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
}
catch(Exception ex)
{
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {}
}
// Show response on activity
content.setText( text );
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
это код php
<?php
$name = urldecode($_POST['name']);
$user = urldecode($_POST['user']);
$email = urldecode($_POST['email']);
$pass = urldecode($_POST['pass']);
print " ==== POST DATA =====
Name : $name
Email : $email
User : $user
Pass : $pass";
?>
1. Вы не можете выполнять веб-запрос в потоке пользовательского интерфейса. Google AsyncTask.
2.Пожалуйста, запросите разрешение на интернет в вашем AndroidManifest.xml, например:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />