简体   繁体   中英

I tried making web-based dictionary in android.I have an api with key which i have included in the code

I'm getting error in this line abc = EditText.getText() + ""; . This whole decodeResultIntoJSON method is an error. Please give me a solution or an alternative.

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);
}

This is the method for sending the request.

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);
}

Here i'am trying to convert the response string into JSON string and then trying to print the result.

private void  decodeResultIntoJSON(String response) {
JSONObject jo = new JSONObject(response);

String definition;
definition = jo.getString("response");
System.out.println(definition);


}

My .xml file is okay.

<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>

change EditText to getText

getText =(EditText)findViewById(R.id.getText);
abc = getText.getText() + ""; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM