【发布时间】:2015-03-24 08:57:53
【问题描述】:
1) 在 netbeans 中使用 json 实现 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 客户端的建议
提前感谢您的帮助
【问题讨论】:
-
这是哪一行?
MainActivity.java:114 -
与您的代码无关,而是一般建议,您应该使用 Retrofit 在您的 android 应用程序中拥有一个不错的 REST API。它功能强大,经过良好测试且直截了当。 square.github.io/retrofit
-
114 行:categoryname = json.getString("CategoryName");
-
@Hugo Gresse,谢谢,也会尝试改造
-
你瞎了吗?
...http://localhost:8084 refused...或者你阅读有问题?服务器在设备/模拟器上吗?
标签: java android json web-services