当我尝试运行我的代码时,出现以下错误。
我用谷歌搜索了它。许多人谈到 onclick 方法不适用于 API<=3。我重新检查了我的 API 级别。它是 17。
我如何开始运行我的代码。
如果需要,我可以发布我的 MainActivity.java 代码来弄清楚。
这是代码
package com.example.myfirstapp;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button btn;
private EditText edit;
private Spinner spinner;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
edit = (EditText) findViewById(R.id.edit_message);
spinner = (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
//Log.v("EditText", edit.getText().toString());
//Log.v("Spinner", spinner.getSelectedItem().toString());
String url="http://js-server.xxx.edu:23405/examples/servlet/HelloWorldExample?title="+edit.getText().toString()+"&type="+spinner.getSelectedItem().toString();
Log.v("url",url);
}
});
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.title_type,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//String url="http://js-server.xxx.edu:23405/examples/servlet/HelloWorldExample?title="+edit.getText().toString()+"&type="+spinner.getSelectedItem().toString();
// Log.v("url",url);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://js-server.xxx.edu:23405/examples/servlet/HelloWorldExample?title="+edit.getText().toString()+"&type="+spinner.getSelectedItem().toString() });
}
}
我要做的就是获取服务器返回的 JSON 数据。然后我需要解析 JSON 数据以形成表格格式。我还没有进入这部分,仍然坚持从我的服务器检索 JSON 数据。
我的 XML 文件是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_message"
android:layout_width="298dp"
android:layout_height="wrap_content"
android:layout_marginTop="37dp"
android:ems="10"
android:hint="@string/edit_message" />
<Spinner
android:id="@+id/spinner"
android:layout_width="304dp"
android:layout_height="45dp" />
<Button
android:id="@+id/button"
style="?android:attr/buttonStyleSmall"
android:layout_width="106dp"
android:layout_height="wrap_content"
android:onclick="readWebpage"
android:text="@string/button_send" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Example Text" />
</LinearLayout>