1)使用json从netbeans实现web服务,该服务从MySQL数据库中获取数据
2)在android中创建rest客户端以调用web服务
已编辑的代码
我的清单文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
jsonParse.java
package android.project.srt.demojson;
public class JsonParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JsonParser() {
}
public String getJSONFromUrl(String url) {
// Making HTTP request
try {
HttpGet get = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream inputStream=entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream),8 * 1024);
String line=null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
} catch (UnsupportedEncodingException e) {
Log.e("unsupported ","encoding exception" +e.getMessage());
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.e("clientprotocolexception ","" + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
Log.e("io","Exception " + e.getMessage());
e.printStackTrace();
}
return sb.toString();
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
private static String url = "http://192.168.1.31:8084/adminWebservice/mywebservice/SelectRestroTypes";
String categoryname;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
//new JSONParse().execute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
tv1.setText("Getting Values Pls wait..");
JsonParser jParser = new JsonParser();
// Getting JSON from URL
// JSONObject json = jParser.getJSONFromUrl(url);
String jsonString=jParser.getJSONFromUrl(url);
tv1.setText(jsonString);
}
}
logcat显示了这个唯一的错误:
03-24 19:11:20.914 30629-30629/android.project.srt.demojson E/ViewRootImpl﹕ sendUserActionEvent() mView == null
欢迎任何有关在Android中创建json客户端的建议
提前感谢您的帮助