【发布时间】:2023-03-13 00:35:01
【问题描述】:
我正在尝试对测试 URL-“http://www.sample.com/interntest.php”发出 POST 请求,负载为 {“password”:“test12345”}
public class VisitorActivity extends AppCompatActivity {
private BitmapDrawable bitmapObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visitor);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
BitmapDrawable bitmapObject=null;
new JSONTask().execute("http://www.sample.com/interntest.php");
LinearLayout i = (LinearLayout)findViewById(R.id.backgroundImg);
if (i != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
i.setBackground(bitmapObject);
}
}
}
public class JSONTask extends AsyncTask<String, String, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection= (HttpURLConnection) url.openConnection();
String password="test12345";
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
//connection.addRequestProperty("password", "test12345");
DataOutputStream dbStrem= new DataOutputStream(connection.getOutputStream());
dbStrem.writeBytes(password);
connection.connect();
dbStrem.flush();
dbStrem.close();
InputStream stream=connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line="";
//StringBuilder responseOutput = new StringBuilder();
/*String password="test12345";
responseOutput.append("password: "+password);*/
System.out.println("output===============");
while((line=reader.readLine())!=null) {
buffer.append(line);
}
String finalJSON= buffer.toString();
JSONObject parentObject = new JSONObject(finalJSON);
Log.d("parentObject", String.valueOf(parentObject));
String backgroundURL= parentObject.getString("backgroundURL");
Log.d("IMGBACKGROUND",backgroundURL+backgroundURL);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(backgroundURL).getContent());
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if(connection!=null) {
connection.disconnect();
}
if(reader!=null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
}
}
}
但在这里我无法在 httpUrlConnection 中添加参数。我知道如何使用 httpClient 添加参数,但不想使用它,因为它不再受支持。我可以知道这样做的更好方法吗?
【问题讨论】:
-
使用volley库或者retrofit库,你可以随意发布参数。
-
已弃用。为 Web 服务使用 Retrofit 或 Volley 库 - (Rest Api)
-
难道不可以像我们过去使用 httpCLient 那样不使用这些库吗?
-
@Rob 您写入输出流的密码字符串应类似于
password=test12345,您也可以尝试在连接上设置内容类型:connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");