Moh*_*men 5 python android pythonanywhere
我正在开发一个Android应用程序,它使用HTTP请求和JSON 从pythonanywhere托管的python服务器发送和接收数据.
该应用程序通过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);
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
当请求发送
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());
Run Code Online (Sandbox Code Playgroud)
经过 3 周的调试并查看服务器的日志文件和客户端的日志文件,我没有发现任何逻辑错误发生,所以我返回查看我发现的pythonanywhere免费服务器限制:
从您的代码访问外部互联网站点,例如 urllib 或 wget
仅通过 HTTP(S) 的特定站点
我不知道 point 是什么意思,但我决定使用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());
Run Code Online (Sandbox Code Playgroud)
但我仍然不知道pythonanywhere免费服务器是否允许您仅发送请求https为什么http请求只能通过 wifi 完美工作。
| 归档时间: |
|
| 查看次数: |
1309 次 |
| 最近记录: |