为什么 Android Studio 模拟器不是 运行 我的 POST 请求的一部分 (Java)
Why does Android Studio Emulator not run part of my POST request (Java)
你好 Whosebug 用户,
我正在尝试编写一个在单击按钮时调用的 post 请求(发送 SMS)。 post 请求有:
一个URL参数:api_key
一个 Header: "Content-Type" = "application/x-www-form-urlencoded"
三个 JSON 对:("from", "insert#here"), ("to", "insert#here"), 和 ("body", "Hello, World")
通过将我的 POST 请求发送到 REST API 端点 URL,我希望在用户单击按钮时发送 SMS。这是我的:
public class PostSMS extends AsyncTask<Void, String, String>
{
@Override
protected String doInBackground(Void... params) {
try {
MainActivity.text.setText("enteredTry");
URL url = new URL("https://api.apidaze.io/apikey/sms/send?api_secret=apisecret");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
MainActivity.text.setText("connectionBuilt");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestMethod("POST");
MainActivity.text.setText("connectionEstablished1");
urlConnection.connect();
MainActivity.text.setText("connectionEstablished23");
// Create JSONObject Request
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("from", "1111111111");
jsonRequest.put("to", "1111111112");
jsonRequest.put("body", "Hello, World");
MainActivity.text.setText("jsonArrayConstructed");
// Write Request to output stream to server
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
out.write(jsonRequest.toString());
out.close();
MainActivity.text.setText("OutputStream written");
int statusCode = urlConnection.getResponseCode();
String statusMsg = urlConnection.getResponseMessage();
// Connection success. Proceed to fetch the response.
if (statusCode == 200)
{
InputStream it = new BufferedInputStream(urlConnection.getInputStream());
InputStreamReader read = new InputStreamReader(it);
BufferedReader buff = new BufferedReader(read);
StringBuilder dta = new StringBuilder();
String chunks;
while ((chunks = buff.readLine()) != null)
{
dta.append(chunks);
}
String returndata = dta.toString();
return returndata;
}
catch (ProtocolException e)
{
e.printStackTrace();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
在 OnClickListener 中 运行 执行此代码后,我的 TextView object(代码中的 text)仅显示 "connectionEstablished1"模拟器,所以它不会 运行 程序的其余部分。
之后
urlConnection.setRequestMethod("POST");
MainActivity.text.setText("connectionEstablished1");
是运行,
该程序不再 运行。没有 compile-time 个错误。
我有
<uses-permission android:name="android.permission.INTERNET" />
也许可。
谢谢!
我检查了你的代码,如果你删除 MainActivity.text.setText(),它就可以工作。
正如 Mike M. 指出的那样,您不应使用 MainActivity.text.setText() 来跟踪进度,您应该为此使用 Logging。如果您想向用户显示进度,您应该使用可以在 AsyncTask 中覆盖的 onProgressUpdate(String... values) 和 onPostExecute(String s)。
一旦您更加熟悉 Android 工作室界面,您也可以尝试 debug your app,这比从 catch 块检查日志和 StackTraces 更容易。
你好 Whosebug 用户,
我正在尝试编写一个在单击按钮时调用的 post 请求(发送 SMS)。 post 请求有:
一个URL参数:api_key
一个 Header: "Content-Type" = "application/x-www-form-urlencoded"
三个 JSON 对:("from", "insert#here"), ("to", "insert#here"), 和 ("body", "Hello, World")
通过将我的 POST 请求发送到 REST API 端点 URL,我希望在用户单击按钮时发送 SMS。这是我的:
public class PostSMS extends AsyncTask<Void, String, String>
{
@Override
protected String doInBackground(Void... params) {
try {
MainActivity.text.setText("enteredTry");
URL url = new URL("https://api.apidaze.io/apikey/sms/send?api_secret=apisecret");
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
MainActivity.text.setText("connectionBuilt");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestMethod("POST");
MainActivity.text.setText("connectionEstablished1");
urlConnection.connect();
MainActivity.text.setText("connectionEstablished23");
// Create JSONObject Request
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("from", "1111111111");
jsonRequest.put("to", "1111111112");
jsonRequest.put("body", "Hello, World");
MainActivity.text.setText("jsonArrayConstructed");
// Write Request to output stream to server
OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
out.write(jsonRequest.toString());
out.close();
MainActivity.text.setText("OutputStream written");
int statusCode = urlConnection.getResponseCode();
String statusMsg = urlConnection.getResponseMessage();
// Connection success. Proceed to fetch the response.
if (statusCode == 200)
{
InputStream it = new BufferedInputStream(urlConnection.getInputStream());
InputStreamReader read = new InputStreamReader(it);
BufferedReader buff = new BufferedReader(read);
StringBuilder dta = new StringBuilder();
String chunks;
while ((chunks = buff.readLine()) != null)
{
dta.append(chunks);
}
String returndata = dta.toString();
return returndata;
}
catch (ProtocolException e)
{
e.printStackTrace();
} catch (MalformedURLException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
在 OnClickListener 中 运行 执行此代码后,我的 TextView object(代码中的 text)仅显示 "connectionEstablished1"模拟器,所以它不会 运行 程序的其余部分。
之后urlConnection.setRequestMethod("POST");
MainActivity.text.setText("connectionEstablished1");
是运行,
该程序不再 运行。没有 compile-time 个错误。
我有
<uses-permission android:name="android.permission.INTERNET" />
也许可。
谢谢!
我检查了你的代码,如果你删除 MainActivity.text.setText(),它就可以工作。
正如 Mike M. 指出的那样,您不应使用 MainActivity.text.setText() 来跟踪进度,您应该为此使用 Logging。如果您想向用户显示进度,您应该使用可以在 AsyncTask 中覆盖的 onProgressUpdate(String... values) 和 onPostExecute(String s)。
一旦您更加熟悉 Android 工作室界面,您也可以尝试 debug your app,这比从 catch 块检查日志和 StackTraces 更容易。