通过移动数据发送 POST 请求
Send POST request over Mobile Data
我正在开发一个 Android 应用程序,它使用 HTTP 请求从 pythonanywhere 中托管的 python 服务器发送和接收数据,并且JSON.
该应用程序通过 WIFI 运行完美,但当我通过移动数据使用它时出现问题。来自带有 GET 请求的服务器的所有数据工作正常,但 POST 和 DELETE 请求似乎没有发送或以其他方式工作。
不知道是不是问题
- 免费服务器
- 不受信任的应用程序
- 权限
PostRequest.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public abstract class PostRequest extends AsyncTask<String,Void,String> {
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
@Override
protected String doInBackground(String... params) {
InputStream inputstream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
StringEntity se = new StringEntity(params[1]);
post.setEntity(se);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(post);
inputstream = httpResponse.getEntity().getContent();
if(inputstream != null)
result = convertInputStreamToString(inputstream);
else
result = "Did not work!";
} catch (Exception e) {
Log.i("[ error stream ]", e.toString());
}
return result;
}
@Override
abstract public void onPostExecute(String result);
}
RequestAddNewStory.java
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.Toast;
import com.shortstory.activities.AddHashtag;
import com.shortstory.api.PostRequest;
public class RequestAddNewStory extends PostRequest {
Context context ;
Button AddHashtag ;
public RequestAddNewStory(Context context , Button AddHashtag)
{
this.context = context ;
this.AddHashtag = AddHashtag ;
}
@Override
public void onPostExecute(String result) {
JSONObject json;
try {
json = new JSONObject(result);
String s = json.getString("message");
if (s.equals("done")) {
Toast.makeText(context,"now add hashtags", Toast.LENGTH_LONG).show();
Intent Addhashtag = new Intent(context , AddHashtag.class);
context.startActivity(Addhashtag);
((Activity) context).finish();
}else
{
AddHashtag.setEnabled(true);
Toast.makeText(context,"can't add story", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
请求发送时
json.accumulate("user_name", "mkm");
json.accumulate("title", "hello");
json.accumulate("body", "hello all");
json.accumulate("img", " ");
RequestAddNewStory rans = new RequestAddNewStory(this , AddHashtag);
rans.execute("http://-----.pythonanywhere.com/api/story" , json.toString());
经过 3 周的调试并查看服务器的日志文件和客户端的日志猫,我没有发现任何逻辑错误发生所以我 return 看到 pythonanywhere我发现的免费服务器限制:
从您的代码访问外部互联网站点,例如urllib 或 wget
仅通过 HTTP(S) 的特定站点
我不知道点是什么意思,但我决定使用 https 而不是 http,它在 wifi 和移动数据
json.accumulate("user_name", "mkm");
json.accumulate("title", "hello");
json.accumulate("body", "hello all");
json.accumulate("img", " ");
RequestAddNewStory rans = new RequestAddNewStory(this , AddHashtag);
rans.execute("https://-----.pythonanywhere.com/api/story" , json.toString());
但我仍然不知道 pythonanywhere 免费服务器是否只允许您发送请求 https 为什么 http 请求仅通过 wifi 完美运行。
我正在开发一个 Android 应用程序,它使用 HTTP 请求从 pythonanywhere 中托管的 python 服务器发送和接收数据,并且JSON.
该应用程序通过 WIFI 运行完美,但当我通过移动数据使用它时出现问题。来自带有 GET 请求的服务器的所有数据工作正常,但 POST 和 DELETE 请求似乎没有发送或以其他方式工作。
不知道是不是问题
- 免费服务器
- 不受信任的应用程序
- 权限
PostRequest.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
public abstract class PostRequest extends AsyncTask<String,Void,String> {
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
@Override
protected String doInBackground(String... params) {
InputStream inputstream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
StringEntity se = new StringEntity(params[1]);
post.setEntity(se);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(post);
inputstream = httpResponse.getEntity().getContent();
if(inputstream != null)
result = convertInputStreamToString(inputstream);
else
result = "Did not work!";
} catch (Exception e) {
Log.i("[ error stream ]", e.toString());
}
return result;
}
@Override
abstract public void onPostExecute(String result);
}
RequestAddNewStory.java
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.Toast;
import com.shortstory.activities.AddHashtag;
import com.shortstory.api.PostRequest;
public class RequestAddNewStory extends PostRequest {
Context context ;
Button AddHashtag ;
public RequestAddNewStory(Context context , Button AddHashtag)
{
this.context = context ;
this.AddHashtag = AddHashtag ;
}
@Override
public void onPostExecute(String result) {
JSONObject json;
try {
json = new JSONObject(result);
String s = json.getString("message");
if (s.equals("done")) {
Toast.makeText(context,"now add hashtags", Toast.LENGTH_LONG).show();
Intent Addhashtag = new Intent(context , AddHashtag.class);
context.startActivity(Addhashtag);
((Activity) context).finish();
}else
{
AddHashtag.setEnabled(true);
Toast.makeText(context,"can't add story", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
请求发送时
json.accumulate("user_name", "mkm");
json.accumulate("title", "hello");
json.accumulate("body", "hello all");
json.accumulate("img", " ");
RequestAddNewStory rans = new RequestAddNewStory(this , AddHashtag);
rans.execute("http://-----.pythonanywhere.com/api/story" , json.toString());
经过 3 周的调试并查看服务器的日志文件和客户端的日志猫,我没有发现任何逻辑错误发生所以我 return 看到 pythonanywhere我发现的免费服务器限制:
从您的代码访问外部互联网站点,例如urllib 或 wget
仅通过 HTTP(S) 的特定站点
我不知道点是什么意思,但我决定使用 https 而不是 http,它在 wifi 和移动数据
json.accumulate("user_name", "mkm");
json.accumulate("title", "hello");
json.accumulate("body", "hello all");
json.accumulate("img", " ");
RequestAddNewStory rans = new RequestAddNewStory(this , AddHashtag);
rans.execute("https://-----.pythonanywhere.com/api/story" , json.toString());
但我仍然不知道 pythonanywhere 免费服务器是否只允许您发送请求 https 为什么 http 请求仅通过 wifi 完美运行。