package com.exmple.moodle_webservice;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
Button token;
JSONParser jParser = new JSONParser();
// Progress Dialog
private ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
token=(Button)findViewById(R.id.button1);
token.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String result = null;
try {
result = new getToken().execute("").get();
}
catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Error--->",e.toString());
}
Log.e("result--->",result);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class getToken extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
String getToken = "http://10.0.2.2:80/moodle/login/token.php";
//String getToken = "http://localhost/moodle/login/token.php? username=niranga&password=S12345s@&service=moodle_mobile_app";
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "niranga"));
params.add(new BasicNameValuePair("password", "S12345s@"));
params.add(new BasicNameValuePair("service", "moodle_mobile_app"));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(getToken, "GET", params);
Log.e("json--->",json.toString());
try {
return json.getString("token");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("Errorrr--->",e.toString());
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
}
}
}
我的 JSONParser 类:
package com.exmple.moodle_webservice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
//Log.e("get Entity result--->",is.toString());
}
} catch (Exception e) {
Log.e("Error-------->>>",e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
当我尝试从 moodle webservice 获取令牌时,出现错误:
日志猫
03-17 05:21:22.285: E/json--->(2008): {"stacktrace":null,"error":"Invalid url or port.","debuginfo":null,"errorcode":null}
03-17 05:21:22.295: E/Errorrr--->(2008): org.json.JSONException: No value for token
03-17 05:21:22.335: E/AndroidRuntime(2008): FATAL EXCEPTION: main
03-17 05:21:22.335: E/AndroidRuntime(2008): java.lang.NullPointerException: println needs a message
03-17 05:21:22.335: E/AndroidRuntime(2008): at android.util.Log.println_native(Native Method)
03-17 05:21:22.335: E/AndroidRuntime(2008): at android.util.Log.e(Log.java:231)
03-17 05:21:22.335: E/AndroidRuntime(2008): at com.exmple.moodle_webservice.MainActivity$1.onClick(MainActivity.java:64)
03-17 05:21:22.335: E/AndroidRuntime(2008): at android.view.View.performClick(View.java:4202)
03-17 05:21:22.335: E/AndroidRuntime(2008): at android.view.View$PerformClick.run(View.java:17340)
03-17 05:21:22.335: E/AndroidRuntime(2008): at android.os.Handler.handleCallback(Handler.java:725)
03-17 05:21:22.335: E/AndroidRuntime(2008): at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 05:21:22.335: E/AndroidRuntime(2008): at android.os.Looper.loop(Looper.java:137)
03-17 05:21:22.335: E/AndroidRuntime(2008): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-17 05:21:22.335: E/AndroidRuntime(2008): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 05:21:22.335: E/AndroidRuntime(2008): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 05:21:22.335: E/AndroidRuntime(2008): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-17 05:21:22.335: E/AndroidRuntime(2008): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-17 05:21:22.335: E/AndroidRuntime(2008): at dalvik.system.NativeStart.main(Native Method)
如何从moodle获得令牌?