为什么Android Studio Emulator不运行部分我的POST请求(Java)
各位用户好 我正在尝试写一个post请求(发送一条短信),点击一个按钮就会调用它。该职位要求包括: 一个URL参数:api_密钥 一个标题:“内容类型”=“应用程序/x-www-form-urlencoded” 三对JSON:(“from”,“insert#here”),(“to”,“insert#here”)和(“body”,“Hello,World”) 通过将POST请求发送到RESTAPI端点URL,我希望在用户单击按钮时发送SMS。这就是我所拥有的:为什么Android Studio Emulator不运行部分我的POST请求(Java),java,android,android-studio,android-emulator,http-post,Java,Android,Android Studio,Android Emulator,Http Post,各位用户好 我正在尝试写一个post请求(发送一条短信),点击一个按钮就会调用它。该职位要求包括: 一个URL参数:api_密钥 一个标题:“内容类型”=“应用程序/x-www-form-urlencoded” 三对JSON:(“from”,“insert#here”),(“to”,“insert#here”)和(“body”,“Hello,World”) 通过将POST请求发送到RESTAPI端点URL,我希望在用户单击按钮时发送SMS。这就是我所拥有的: public class PostS
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;
}
在运行时
该程序不再运行。没有编译时错误
我有
<uses-permission android:name="android.permission.INTERNET" />
还有许可
谢谢 我检查了你的代码,如果你删除MainActivity.text.setText(),代码就正常了 正如Mike M.指出的,您不应该使用MainActivity.text.setText()来跟踪进度,您应该使用。若要向用户显示进度,应使用可在AsyncTask中重写的onProgressUpdate(字符串…值)和onPostExecute(字符串s)
此外,您还可以尝试在熟悉Android studio界面后,检查catch块中的日志和堆栈跟踪比检查catch块中的日志和堆栈跟踪更容易。检查日志中是否有任何可能从这些
catch
块打印的内容。您不应该直接从doInBackground()
触摸UI;i、 例如,你不应该在那里调用MainActivity.text.setText()
Hi@Mike M.,我是Android Studio的新手,所以界面也有点新。我在哪里可以从catch块检查日志?看看。我经常记录日志,现在我收到一个错误的请求运行时错误。您是否有关于原因的推测?尝试将from和to值作为int:jsonRequest.put(“from”,1111)发送;并检查您的请求url(确切的api实例和url格式)。我让程序在那里运行。但在复制和粘贴代码到方法中,导入必要的依赖项并运行时,我得到了错误“Item可能不为null”,并且Item被写入依赖项的一个类中。不过我会尽力照你说的做。我将更改电话号码的数据类型。等待进一步更新。将字符串电话号码更改为整数无效。这个url看起来也不错。你好@P.Suvajac,多亏了你的日志记录方法,我才让它正常工作。我发现我使用了错误的Apache客户机形式,因此我必须创建一个DefaultHttpClient()(尽管它已被弃用,但仍然有效),而不是在这里建立的连接。
<uses-permission android:name="android.permission.INTERNET" />