我在这一行abc = EditText.getText() + "";
收到错误。
整个decodeResultIntoJSON方法都是错误的。
请给我一个解决方案或替代方案。
public class MainActivity extends ActionBarActivity {
private String abc;
EditText getText;
SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
abc = EditText.getText() + "";
}
});
makePostRequest(abc);
}
这是发送请求的方法。
private void makePostRequest(String word){
BufferedReader bufferedReader = null;
StringBuffer stringBuffer = new StringBuffer("");
HttpClient httpClient= new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.dictionaryapi.com/api/v1/references/collegiate/xml/test?key=6eec6f75-efce-4cd0-9cef-1dd9cd5e905d");
// Url Encoding the POST parameters
try { StringEntity strEntity = new StringEntity(word);
httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>) strEntity));
} catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
}
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
String LineSeparator = System.getProperty("lineSeparator");
while((line=bufferedReader.readLine())!=null)
{stringBuffer.append(line+LineSeparator);}
bufferedReader.close();
// writing response to log
Log.d("Http Response:", response.toString());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
decodeResultIntoJSON(stringBuffer);
}
这里我试图将响应字符串转换为JSON字符串,然后尝试打印结果。
private void decodeResultIntoJSON(String response) {
JSONObject jo = new JSONObject(response);
String definition;
definition = jo.getString("response");
System.out.println(definition);
}
我的.xml文件没问题。
<EditText
android:id="@+id/getText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="100dp"
android:paddingTop="10dp"
/>
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getMe"
android:id="@+id/searchView"
/>
</LinearLayout>
答案 0 :(得分:0)
将getText
更改为getText =(EditText)findViewById(R.id.getText);
abc = getText.getText() + "";
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
var cardnumber = ["#card1", "#card2", "#card3", "#card4", "#card5"];
$("#card1, #card2, #card3, #card4, #card5").click(function() {
var y = cardnumber.indexOf("#" + this.id);
alert(y);
});
});
</script>
</head>
<body>
<p id="card1">Click me!</p>
<p id="card2">Click me!</p>
<p id="card3">Click me!</p>
<p id="card4">Click me!</p>
<p id="card5">Click me!</p>
</body>
</html>