我正在从Android向php服务器发送一些数据。但是httpurlconnection无法正常工作。我认为我的网址未发生连接。由于我的计算机不支持模拟,因此无法在移动设备上测试构建apk时调试代码。我正在按Toast进行检查,但是在连接后,它既未显示CONNECTED,也未显示NOT CONNECTED Toast。我的url连接代码有什么问题吗,或者我必须在php服务器端做一些事情才能接受来自Android应用程序的连接?
package com.it.jumtech.equestion;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import android.app.AlertDialog;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
Button button_login;
EditText edit_uid,edit_pwd;
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
button_login = (Button)findViewById(R.id.button_login);
edit_uid = (EditText)findViewById(R.id.edit_uid);
edit_pwd = (EditText)findViewById(R.id.edit_pwd);
builder = new AlertDialog.Builder(this);
// onclick button this function will execute
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("uid", edit_uid.getText().toString());
jsonObject.put("pwd", edit_pwd.getText().toString());
String logininfo = jsonObject.toString();
Toast.makeText(LoginActivity.this,
"Json Built = " + logininfo, Toast.LENGTH_LONG).show();
String link = "http://www.example.com/login_chk.php";
URL url = new URL(link);
HttpURLConnection conn = null;
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
Toast.makeText(LoginActivity.this,
"Connected", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LoginActivity.this,
"Not Connected", Toast.LENGTH_LONG).show();
}
OutputStream os = conn.getOutputStream();
Toast.makeText(LoginActivity.this,
"ostream made", Toast.LENGTH_LONG).show();
BufferedWriter bw = new BufferedWriter(new
OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("param_logininfo", "UTF-8") + "=" +
URLEncoder.encode(logininfo, "UTF-8");
bw.write(data);
Toast.makeText(LoginActivity.this,
data+"written to url", Toast.LENGTH_LONG).show();
bw.flush();
bw.close();
os.close();
Toast.makeText(LoginActivity.this,
"Json Sent", Toast.LENGTH_LONG).show();
int respcode = conn.getResponseCode();
Toast.makeText(LoginActivity.this,
"Response code = "+respcode, Toast.LENGTH_LONG).show();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new
InputStreamReader(is,"UTF-8"));
StringBuffer sb = new StringBuffer();
String line = null;
// read server output
Toast.makeText(LoginActivity.this,
"Read from server", Toast.LENGTH_LONG).show();
while((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
String result = sb.toString();
Toast.makeText(LoginActivity.this,
"Success return from server", Toast.LENGTH_LONG).show();
Toast.makeText(LoginActivity.this,
result, Toast.LENGTH_LONG).show();
}catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
答案 0 :(得分:1)
您需要添加特殊权限才能发送HTTP请求。 Android默认会阻止简单HTTP,因为它们被认为不安全。
您的清单应包含:
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>
如果您未设置此权限,则android会引发IOException
。
此外,您似乎在主线程上进行网络连接,这是不允许的。
使用工作线程或AsyncTask
来阻止操作,例如HTTP请求。