我试图发送数据,并在我得到它后,我试图创建一个elemenet,但有错误,如:
错误:(121,48)错误:不兼容的类型:readerActivity.SendPostRequest无法转换为上下文
但没有从网站获取此数据 - 工作正常
public class SendPostRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try {
String book = getIntent().getExtras().getString("book");
String page = "2";
URL url = new URL("http://website.org/test"); // here is your URL path
JSONObject postDataParams = new JSONObject();
postDataParams.put("one", book);
postDataParams.put("two", page);
Log.e("params",postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new
InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
LinearLayout llMain = (LinearLayout) findViewById(R.id.activity_reader);
String text = result;
String[] parts = text.split("</br></br>");
for (String part : parts)
{
FlowLayout ab = new FlowLayout(this);
llMain.addView(ab);
ab.setPadding(1,1,1,35);
FlowLayout fab = new FlowLayout(this);
ab.addView(fab);
String[] words = part.split(" ");
for (String word : words)
{
TextView btnNew = new TextView(this);
btnNew.setText(word.toString());
ab.addView(btnNew);
btnNew.setPadding(20,15,20,25);
btnNew.setTextSize(19);
}
}
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
答案 0 :(得分:0)
完成了,我刚刚改变了
FlowLayout ab = new FlowLayout(this);
到
FlowLayout ab = new FlowLayout(getApplicationContext());
并且工作正常