所以我从这里获得了源代码来进行测试,并且我没有像我应该的那样从服务器获得任何输出或响应。有人知道为什么会这样吗?我有一种感觉,在第二个“尝试”中,某项不起作用,并且没有任何内容进入执行。这样对吗?无论如何,请帮我解决这个问题,以便给我答复/输出。
public class HttpPostExample extends Activity {
TextView content;
EditText fname, email, login, pass;
String Name, Email, Login, Pass;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_post_example);
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! " );
}
}
});
}
// Create GetText Metod
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://androidexample.com/media/webservice/httppost.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 );
}
}
编辑:我只是做了一些调试,发现我是对的问题是什么,它在这里的某个地方,有人知道确切在哪里吗?
URL url = new URL("http://androidexample.com/media/webservice/httppost.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();
}
您正在主(UI)线程上进行网络I / O。这在Android中是不允许的。
您应该将所有代码移到AsyncTask
。但是请记住,您不能从后台线程更改UI,因此显示结果的部分必须在中执行onPostExecute()
。
例如,更改您的GetText()
方法以返回带有文本的字符串,而不是调用content.setText()
,然后将您的点击监听器变为类似以下内容:
public void onClick(View v)
{
// Get user defined values
Name = fname.getText().toString();
Email = email.getText().toString();
Login = login.getText().toString();
Pass = pass.getText().toString();
new AsyncTask<Void, Void, String>()
{
@Override
public String doInBackground (Void... params)
{
return GetText();
}
@Override
protected void onPostExecute(String result)
{
content.setText(result);
}
}.execute();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句