我正在向服务器发送JSON字符串。从服务类调用Postdata类,其中服务类是从MainActivity中的onCreate()方法启动的。我之前测试过PostData
类的代码并且运行良好。我刚收到以下警告,我不再收到服务器的响应。这是什么意思?
我感谢任何帮助
警告:
05-30 19:15:22.791: W/System.err(8371): java.io.FileNotFoundException: https://bustracker.rhcloud.com/webapi/test
05-30 19:15:22.791: W/System.err(8371): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
05-30 19:15:22.791: W/System.err(8371): at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
05-30 19:15:22.801: W/System.err(8371): at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
05-30 19:15:22.801: W/System.err(8371): at com.bustracker.PostData$MyAsyncTask.doInBackground(PostData.java:83)
05-30 19:15:22.801: W/System.err(8371): at com.bustracker.PostData$MyAsyncTask.doInBackground(PostData.java:1)
05-30 19:15:22.801: W/System.err(8371): at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-30 19:15:22.801: W/System.err(8371): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-30 19:15:22.801: W/System.err(8371): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-30 19:15:22.801: W/System.err(8371): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-30 19:15:22.801: W/System.err(8371): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-30 19:15:22.811: W/System.err(8371): at java.lang.Thread.run(Thread.java:818)
PostData类:
public class PostData {
String jSONString;
private AsyncTaskCallback callback;
public PostData(AsyncTaskCallback callback) {
this.callback = callback;
}
public String getjSONString() {
return jSONString;
}
public void setjSONString(String jSONString) {
this.jSONString = jSONString;
}
public void post_data(String jSONString, Context context) {
this.jSONString = jSONString;
new MyAsyncTask(context).execute(jSONString);
}
class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<Integer>> {
final Context mContext;
ArrayList<Integer> routes = new ArrayList<Integer>();
double distance;
public MyAsyncTask(Context context) {
mContext = context;
}
@Override
protected ArrayList<Integer> doInBackground(String... params) {
BufferedReader reader = null;
try {
System.out.println("The output of : doInBackground "
+ params[0]);
URL myUrl = new URL(
"https://bustracker.rhcloud.com/webapi/test");
HttpURLConnection conn = (HttpURLConnection) myUrl
.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestProperty("Content-Type", "application/json");
conn.connect();
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Gson gson = new Gson();
JSONStore data = gson.fromJson(sb.toString(), JSONStore.class);
routes = data.getRoutes();
distance = data.getDistance();
System.out.println("The output of the StringBulder: "
+ sb.toString());
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
protected void onPostExecute(ArrayList<Integer> result) {
if (routes != null && !routes.isEmpty()) {
callback.onAsyncTaskFinished(routes, distance);
}else{
Log.e("123", "Avoiding null pointer in the PostData class, the routes are null!!!");
}
}
}
}