Код: Выделить всё
public class MainActivity extends Activity implements OnClickListener {
public Context context;
SharedPreferences mSettings;
public static final String APP_PREFERENCES = "mysettings";
String str2;
String str3;
MainActivity()
{}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button postvk =(Button)findViewById(R.id.btn);
postvk.setOnClickListener(this);
context = getApplicationContext();
WebView wv = (WebView) this.findViewById(R.id.webview);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
wv.setHorizontalScrollBarEnabled(false);
webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
wv.setWebViewClient(new VkWebViewClient( context));
/* Intent intent = new Intent(this, ListViewActivity.class);
startActivity(intent);*/
// wv.loadUrl("https://oauth.vk.com/authorize?client_id=4922445&scope=wall,offline&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&v=5.5&response_type=token&revoke=1");
wv.loadUrl("https://oauth.vk.com/authorize?client_id=4922445&scope=messages,offline&redirect_uri=https://oauth.vk.com/blank.html&display=mobile&v=5.5&response_type=token&revoke=1");
// new VkPostTask(context).execute();
// создаем обработчик нажатия
OnClickListener oclBtnOk = new OnClickListener() {
@Override
public void onClick(View v) {
mSettings = context.getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
String savedText = mSettings.getString("Access_token", "");
String savedText1 = mSettings.getString("User_id", "");
if(savedText == "" || savedText1 == "")
{
Log.d("Vkfunc", "Еще пусто");
}
// new VkPostTask(context).execute();
}
};
// присвоим обработчик кнопке OK (btnOk)
// postvk.setOnClickListener(oclBtnOk);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn:
// TODO Call second activity
Intent intent = new Intent(this, ListViewActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
класс MainActivity
Код: Выделить всё
public class VkWebViewClient extends WebViewClient {
private static final String TAG = "Vkfunc";
public String access;
public String us_id;
public String mess;
Context context;
SharedPreferences mSettings;
String str2;
String str3;
public static final String APP_PREFERENCES = "mysettings";
public ArrayList<String> vkmass;
public VkWebViewClient( Context context ) {
// TODO Auto-generated constructor stub
this.context = context;
}
public class VkPostTask extends AsyncTask<Void, Void, Void> {
// Postvk vk;
// Context context;
public VkPostTask(Context context){
// this.context = context;
// vk = new Postvk(this.context);
}
@Override
protected Void doInBackground(Void... params) {
//vk.postvk();
Log.d(TAG, "здесь");
mSettings = context.getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
String savedText = mSettings.getString("Access_token", "");
String savedText1 = mSettings.getString("User_id", "");
if(savedText == "" || savedText1 == "")
{
Log.d("Vkfunc", "Еще пусто");
}
Log.d(TAG, savedText);
String mess = "HelloPostEto";
Log.d(TAG, savedText1);
String oid = "60374011";
String urls = "https://api.vk.com/method/"+
"messages.get?"+
"uid="+ savedText1+
"&access_token="+savedText
;
/*String urls = "https://api.vk.com/method/"+
"messages.getHistory?"+
"user_id="+ oid+
"&offset="+ "1"+
"&count="+ "5"+
"&access_token="+savedText;*/
Log.d(TAG, "здесь");
HttpClient client = new DefaultHttpClient();
// Log.d(TAG, access);
HttpPost request = new HttpPost(urls);
// Log.d(TAG, access);
try {
HttpResponse response = client.execute(request);
Log.d(TAG, "Запрос");
HttpEntity entity = response.getEntity();
Log.d(TAG, "Запрос");
String responseText = EntityUtils.toString(entity);
Log.d(TAG, responseText);
//parse response for error code or not
// errorFlag = parseResponse(responseText);
vkmass = new ArrayList<String>();
//Log.d(Constants.DEBUG_TAG,"response text="+responseText);
JSONObject object = new JSONObject(responseText);
JSONArray array = object.getJSONArray("response");
for (int i = 1; i < array.length(); i++)
{
JSONObject obj=array.getJSONObject(i);
str2=obj.getString("body"); строки которые нужно добавить в ListView ListViewActivity
str3 = obj.getString("uid"); строки которые нужно добавить в ListView ListViewActivity
Log.d(TAG, str2);
Log.d(TAG, str3);
vkmass.add(str3);
vkmass.add(str2);
int size = vkmass.size();
// Log.d("Size: ", "The size "+size);
}
}
catch(ClientProtocolException cexc){
cexc.printStackTrace();
}
catch(IOException ioex){
ioex.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute( Void result) {
super.onPostExecute(result);
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
new VkPostTask(context).execute();
}
@Override
public void onPageFinished(WebView view, String url) {
context = this.context;
// mSettings = context.getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
Editor editor = mSettings.edit();
// Log.i("VkWebViewClient onPageFinished", url);
if (url.contains("oauth.vk.com/blank.html#")) {
if (url.contains("error")) {
// Error
} else {
String ahrore = url.substring(url.indexOf("#") + 1);
Log.d(TAG, ahrore);
String access_token = ahrore.substring(ahrore.indexOf("n")+2,ahrore.indexOf("&"));
access = access_token;
//String user_id = null;
Log.d(TAG, access_token);
int i = ahrore.lastIndexOf("=");
String tmpStr = ahrore.substring(i + 1, ahrore.length());
Log.d(TAG, tmpStr);
us_id = tmpStr;
Log.d(TAG, us_id);
boolean errorFlag = true;
editor.putString("Access_token", access);
editor.putString("User_id", us_id);
editor.apply();
//String savedText = mSettings.getString("Acce", "");
//String savedText1 = mSettings.getString("Use", "");
//Log.d("Vkfunc", savedText);
//Log.d("Vkfunc", savedText1);
/* String mess = "Hello Post";
mess = "Hello";
String urls = "https://api.vk.com/method/"+
"wall.post?"+
"uid="+ us_id+
"&message="+mess+
"&access_token="+access
;*/
//new VkPostTask(context).execute();
/* HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(urls);
try {
HttpResponse response = client.execute(request);
Log.d(TAG, "Запрос");
HttpEntity entity = response.getEntity();
Log.d(TAG, "Запрос");
String responseText = EntityUtils.toString(entity);
//parse response for error code or not
// errorFlag = parseResponse(responseText);
//Log.d(Constants.DEBUG_TAG,"response text="+responseText);
}
catch(ClientProtocolException cexc){
cexc.printStackTrace();
}
catch(IOException ioex){
ioex.printStackTrace();
}*/
}
}
}
}
Код: Выделить всё
public class ListViewActivity extends Activity {
Context context;
VkWebViewClient vk;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Vkfunc", "здесь в активити");
context = getApplication();
ListView listView = (ListView) findViewById(R.id.listView1);
vk = new VkWebViewClient(context);
final ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,vk.vkmass);
listView.setAdapter(adapter);
}
}
ListViewActivity активити с ListView
Подскажите как я могу инициализировать адаптер ListView в ListViewActivity отмеченными сообщениями и тем самым вывести их на экран в ListView?? Или подскажите более простой способ вывода данных сообщений на экран?? в рамках данного примера